zoukankan      html  css  js  c++  java
  • Selenium驱动Chrome浏览器

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;

    public class Chrome {
    public static void main(String[] args) throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
    String baiduHomePage;
    baiduHomePage = "https://www.baidu.com/";
    //百度首页的地址

    WebDriver driver;
    //声明一个WebDriver
    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-infobars");
    //消除“Chrome正受到自动测试软件的控制”提示
    options.addArguments
    ("--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default");
    //消除“不安全”提示,启用用户默认的数据目录
    driver =new ChromeDriver(options);
    driver.manage().window().maximize();
    //使浏览器窗口最大化
    driver.get(baiduHomePage);
    //打开百度
    Thread.sleep(2000);
    //强制线程等待2秒钟
    assert driver.getTitle().equals("百度一下,你就知道");
    //断言页面标题

    driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("Selenium");
    //在百度搜索输入框输入“Selenium”
    driver.findElement(By.xpath(".//*[@id='su']")).click();
    //点击搜索按钮
    Thread.sleep(2000);
    assert driver.getTitle().equals("Selenium_百度搜索");

    driver.close();
    //关闭浏览器窗口
    driver.quit();
    //结束dirver
    }
    }

    需要注意的是,
    Chrome浏览器可以是官网上最新的版本,
    驱动的版本是:
    chromedriver_win32
  • 相关阅读:
    r_action
    微内核 客户服务器模式 分布式
    机制与策略分离
    自顶向下设计
    swap
    专人写接口+模型,专人写业务逻辑---interface_model -- business logical
    14days laravel
    t
    不用print调试 xdebug ubuntu phpstorm 远程断点调试
    peewee sqlalchemy
  • 原文地址:https://www.cnblogs.com/yjlch1016/p/8320885.html
Copyright © 2011-2022 走看看