新建一個
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來實現對瀏覽器的控制。