Selenium FirefoxDriver总结

新建一个

WebDriver driver = new FirefoxDriver();

Firefox目录变更

System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla Firefox/firefox.exe");

开URL

Driver.get(url);
Driver.Navigation.to(url);

设置firefox

1.新建一个 firefox -p 一般默认在:

C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles

2.在代码里引用这个profile

File F = new File("C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\9xhxx9r7.Selenium");
FirefoxProfile profile = new FirefoxProfile(file);
WebDriver driver = new FirefoxDriver(profile);

加载插件

File file = new File("files/firebug-2.0.7-fx.xpi");
FirefoxProfile profile = new FirefoxProfile();
try{
    profile.addExtension(file);
}catch(Exception e){
   e.printStackTrace();
}
WebDriver driver = new FirefoxDriver(profile);

通过about:config可以看到firefox的设置,可以通过代码改变, 有些设置直接可以通过firefox profile来设置

        //设置代理参数
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
        
        //设置默认下载路径
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\\");

capabilities的设置

DesiredCapabilities capabilities = new DesiredCapabilities(); 
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new FirefoxDriver(capabilities);

这个capabilities是启动一个session必备的,是对所有driver都可以设置的,而firefoxprofile只有firefox只适用firefox

Driver的timeout设置

页面加载timeout
driver.manage().timeouts().pageLoadTimeout(pageLoadTimeout, TimeUnit.SECONDS);
找对象的timeout,动态找
driver.manage().timeouts().implicitlyWait(waitTimeout, TimeUnit.SECONDS);
脚本执行的timeout
driver.manage().timeouts().setScriptTimeout(scriptTimeout, TimeUnit.SECONDS);

如果想在页面没有加载完成的情况下就点击或者识别元素,用finally

 FirefoxDriver在selenium3的时候已经被弃用了,FirefoxDriver原理是用firefox的add-on来实现对浏览器的控制。

更新后目前用的是geckodriver.exe,是通过geckodriver把W3 WebDriver wire protocol翻译成Marionette可以识别的协议接口,最终通过Marionette来实现对浏览器的控制。

posted on 2015-11-19 21:05  GoldenRazor  阅读(15113)  评论(0编辑  收藏  举报

导航