1、搭建环境注意的问题:
1)selenium和firefox的版本要严格保持一致(比如firefox55对应selenium3.5.1;chrome62对应selenium2.33.0);
a、selenium和chrome映射版本详见:http://blog.csdn.net/huilan_same/article/details/51896672
b、所有chromedriver版本下载:http://chromedriver.storage.googleapis.com/index.html
c、firefox各个版本下载地址:http://ftp.mozilla.org/pub/firefox/releases
d、下载GeckDriver:https://github.com/mozilla/geckodriver/releases
2)firefox48之前不需要指定driver;从firefox48开始mozilla会提供个别driver,叫geckodriver。先在这里下载driver,然后在code设置改driver后启动即可。设置方式:
System.setProperty("webdriver.gecko.driver", "geckodriver.exe的路径");
2、简单java demo:
firefox:
public static void sampleFirefox(){
WebDriver driver = null;
try {
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
//firefox46之后需要手动设置driver
System.setProperty("webdriver.gecko.driver", "D:\\development\\geckodriver-v0.19.1-win64\\geckodriver.exe");
// System.setProperty("sun.net.client.defaultConnectTimeout", "95000");
// System.setProperty("sun.net.client.defaultReadTimeout", "95000");
System.out.println("ready create driver...");
driver = new FirefoxDriver();// 创建一个 firefox 的浏览器实例
System.out.println("create driver...");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
WebElement txtbox = driver.findElement(By.name("wd"));
txtbox.sendKeys("Glen");
WebElement btn = driver.findElement(By.id("su"));
btn.click();
} catch (Exception e) {
System.out.println(e.toString());
} finally {
// driver.quit();//关闭
}
}
Chrome:
public static void sampleChrome() {
WebDriver driver = null;
try {
//解决:seleniumwebdriver启动Chrome之后,会出现一个小黄条提示信息“您使用的是不受支持的命令行标记:--ignore-certificate-error”
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type", "--ignore-certificate-errors");
System.setProperty("webdriver.chrome.driver", "D:\\development\\chromedriver_win32_2.33\\chromedriver.exe");
System.out.println("ready create driver...");
driver = new ChromeDriver(options);// 创建一个 chrome 的浏览器实例
driver.get("http://www.baidu.com/");
//浏览器窗口变大
driver.manage().window().maximize();
System.out.println("start firefox browser succeed...");
} catch (Exception e) {
System.out.println(e.toString());
} finally {
// driver.quit();
}
}
网友评论