Selenium2啟動瀏覽器且載入外掛程式

來源:互聯網
上載者:User

標籤:

一、SELENIUM2啟動瀏覽器

注意: SELENIUM2在啟動瀏覽器時,都是啟動一個乾淨的沒有任務 外掛程式及cookies資訊的瀏覽器,即使是你之前的瀏覽器有設定過代理,到自動化啟動時,也是沒有代理的模式。

1.啟動firefox瀏覽器:

啟動不在預設安裝路徑的firefox瀏覽器:

 

2.啟動chrome瀏覽器:

需要chromedriver.exe的支援,驅動(http://docs.seleniumhq.org/download/)

如果不想用setProperty的方式,也可以將chromedriver.exe 放在”C:\Windows\System32”路徑下並重啟電腦即可

 

3.啟動IE瀏覽器:
需要IEDriverServer.exe 的支援,且 IE的exe檔案分64位與32位,請根據自已的機器選擇相應的exe檔案。
啟動代碼如下:

 

二、SELENIUM2啟動瀏覽器時載入外掛程式

載入外掛程式的作用:有的是為了調試,也有的是被測試的系統本身需要外掛程式

Firefox下載入firebug外掛程式:

Chrome下載入crx外掛程式:

且IE下沒有外掛程式載入。


三、SELENIUM2啟動firefox時的profile設定

在firefox地址欄中輸入about:config,可以看到有哪些可以設定

舉例:匯出檔案的地址設定


3.啟用預設情況下被firefox禁用的功能,以本地事件例,很簡單直接設定為true就可以了。

特別重要:啟動時,每次都是一個乾淨的firefox,如果要啟動本機器的firefox的配置:

如果在機器B上要啟動機器A上的firefox配置,則先匯出A的配置,然後載入:

將A機器上的Profiles檔案夾”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”給拷貝出來

 


四、SELENIUM2啟動chrome時的profile設定

特別重要:將user data檔案夾” C:\Users\cloudchen\AppData\Local\Google\Chrome\User Data”拷貝出來
?
請自己驗證一下,加不加以下代碼的區別:


五、SELENIUM2啟動IE時的設定

特別重要:啟動IE時,需關閉保護模式。 4個紅框標記的地區中的保護模式的checkbox 都要uncheck


?也可以代碼關閉:

  

完整代碼如下:

package com.selenium.test;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebDriver.Navigation;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;import org.openqa.selenium.ie.InternetExplorerDriver;public class launchBrowser {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stublaunchChrome();}public static void launchFireFox() {FirefoxProfile firefoxProfile = new FirefoxProfile();firefoxProfile.setPreference("browser.download.folderList", 2);firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);firefoxProfile.setPreference("browser.download.dir", "d:\\test");firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");String proxyIp = "child-prc.intel.com";int proxyPort = 913;firefoxProfile.setPreference("network.proxy.type", 1);firefoxProfile.setPreference("network.proxy.http", proxyIp);firefoxProfile.setPreference("network.proxy.http_port", proxyPort);firefoxProfile.setPreference("network.proxy.ssl", proxyIp);firefoxProfile.setPreference("network.proxy.ssl_port", proxyPort);firefoxProfile.setPreference("network.proxy.share_proxy_settings", true);firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");WebDriver driver = new FirefoxDriver(firefoxProfile);// WebDriver driver = new FirefoxDriver();Navigation navigation = driver.navigate();navigation.to("https://baidu.com");// driver.close();// driver = null;}public static void launchChrome() {System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");WebDriver driver = new ChromeDriver();Navigation navigation = driver.navigate();navigation.to("https://www.baidu.com");driver.close();driver = null;}public static void launchIE() {System.setProperty("webdriver.ie.driver", "files/iedriver64.exe");WebDriver driver = new InternetExplorerDriver();Navigation navigation = driver.navigate();navigation.to("https://www.baidu.com");driver.close();driver = null;}}

 

Selenium2啟動瀏覽器且載入外掛程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.