selenium学习第一步(firefox安装firebug和firepath插件)
1.? FireBug 是Firefox瀏覽器的插件;
???? FirePath是FireBug插件擴展的一個工具;
2. chrome瀏覽器自帶開發人員工具,可以通過工具——開發人員模式或者直接按快捷鍵F12打開
3. IE瀏覽器從IE8開始加入了開發人員工具,通過工具——開發人員工具模式或者F12打開
4. selenium環境的安裝:
?(1)java的安裝
?(2)JDK(java developer kit) 的安裝
?(3)環境變量的配置:JAVA_HOME:??? JDK的安裝目錄
??????????????????????????????????????????????????????? CLASS_PATH:?? .,%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
?????????????????????????????????????????????????????? PATH:????? %jAVA_HOME%\bin;%JAVA_HOME%\jre\bin
5. 下載需要安裝的java版本的selenium包
下載地址為:http://docs.selenuimhp.org/download/
6. 各個瀏覽器驅動下載
下載地址為:http://www.selenuimhp.org/download/,驅動下載后解壓放置在某個路徑下,然后將該路徑放置在系統環境變量path值中
百度網盤下載地址:
鏈接: http://pan.baidu.com/s/1mhCh3za 密碼: w556
7. 針對selenium3.0以上的版本,需要使用配套的firefox45以上的版本
8.啟動各個瀏覽器的方法:
?1)設置驅動路徑
? Firefox: System.setProperty("webdriver.gecko.driver","D:\\Mozilla Firefox/webdriver/geckodriver.exe"); 第二個值為驅動放置路徑
chrome: System.setProperty("webdriver.chrome.driver","D:\\chromedriver\\chromedriver.exe");
? IE: System.setProperty("webdriver.ie.driver","D:\\IEDriver\\IEDriverServer.exe");
DesiredCapabilities iecap = DesiredCapabilities.internetExplorer();
iecap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(iecap);? ?
Edge:?System.setProperty("webdriver.edge.driver","D:\\EdgeDriver\\MicrosoftWebDriver.exe"); 第二個值為驅動安裝路徑 ?(win10上面)
EdgeOptions options= newEdgeOptions(); ??//設置Edge瀏覽器的option并設置打開頁面策略為:eager
? ? ? ? options.setPageLoadStrategy("Eager");
RemoteWebDriver ?driver= newEdgeDriver(); ? ? ?//聲明driver對象,注意這里用的是RemoteWebDriver
備注:由于MicrosoftEdge是新的瀏覽器,目前做自動化測試還不算合適,微軟也正在逐步完善,這里主要是給大家講解了一個簡單的例子。
?2)如果瀏覽器安裝路徑為非默認的,則需要做如下設置:
Firefox:System.setProperty("webdriver.firefox.bin","D:\\Mozilla Firefox\\firefox.exe");第二個值為瀏覽器安裝路徑
3)使用selenium加載瀏覽器時打開的是一個全新的且不帶任何插件的瀏覽器,解決辦法如下:
firefox:當我們需要在用firefox進行測試時啟動firebug,可以使用下面的代碼
動態設置插件
下載firebug插件:firebug-2.0.19-fx.xpi
File f = new File("./piugins/firebug-2.0.19-fx.xpi"); ?聲明插件位置
FirefoxProfile firefoxprofile = new FirefoxProfile(); ?聲明一個profile對象,里面保存用戶相關信息
firefoxProfile.addExtension(f);
firefoxProfile.setPreference("extensions.firebug.currentVersion","2.0.19");
WebDriver driver = new FirefoxDriver(firefoxProfile);
以此類推,當我們需要啟用別的擴展程序時,換下文件就可以了
啟動默認的瀏覽器
?ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile firefoxProfile = allProfiles.getProfile("default");
有時候某些網站需要用代理模式去訪問,這個時候我們可能需要設置瀏覽器代理,具體在火狐上的實現方式為:
String proxyIp = "192.168.34.122"; int proxyPort = 60000; ? 端口號必須為整數 FirefoxProfile firefoxProfile = new FilefoxProfile(); firefoxProfile.setPreference("network.proxy.type",1); 啟用代理 firefoxProfile.setPreference("network.proxy.http",proxyIp); 設置代理IP firefoxProfile.setProference("newwork.proxy.http_port",proxyPort); 設置端口號
WebDriver driver = new FirefoxDriver(firefoxProfile);
當然你還可以設置ftp,ssl和socks主機代理,具體可以設置哪些代理可以在火狐瀏覽器中輸入:about:config。然后搜索:network.proxy即可知道有哪些類型的代理
chrome:
動態設置插件:
File f =new File("/res/Infinity1.2.1.crx"); 插件位置
ChromeOptions oc = new ChromeOptions();
oc.addExtension(f);
WebDriver driver = new ChromeDriver(oc);
指定用戶數據啟動方式:
(Selenium操作瀏覽器是不加載任何配置的,有時候我們需要使用用戶的數據,比如一些賬戶的登錄記錄,緩存等數據的時候,我們可以選擇完全加載Chrome的用戶數據,如:用戶設置了啟動瀏覽器之后打開對應的頁面;右上角還有兩個插件,這也是用戶安裝的)
ChromeOptions co = new ChromeOptions(); ??聲明chromeoptions,主要是給chrome設置參數
co.addArgument("--user-data-dir=C:\\Users\\Young\\AppData\\Local\\Google\\Chrome\\User Data"); ?//添加用戶配置文件路徑,比如這里我的chrome用戶數據在C:\\Users\\Young\\AppData\\Local\\Google\\Chrome\\User Data
WebDriver driver = new ChromeDriver(co);
?修改User-Agent來偽裝瀏覽器訪問手機站點
(有時候為了測試需要,可能需要使用測試手機wap這樣的站點,如果用真正的手機去測試也可以實現,但是比較麻煩,我們可以通過設置chrome的user agent來偽裝瀏覽器,達到我們的測試目的)
ChromeOptions co = new ChromeOptions();
co.addArgument("--user-agent=iPhone6");后面為所選的機型
總結
以上是生活随笔為你收集整理的selenium学习第一步(firefox安装firebug和firepath插件)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [2]Selenium学习系列----
- 下一篇: 如何成功安装旧版本火狐,成功安装fire