標籤:chrome profile webdriver selenium chrome設定檔
Webdriver 啟動Chrome瀏覽器時,預設是開啟一個新使用者,而非預設使用者,即新使用者沒有我們安裝擴充程式。但在實際應用中,我們會需要 預設使用者安裝的一些擴充程式,比如對於某些js或者css樣式,需要代理才能訪問成功,使用預設使用者就顯得尤為重要(因為你不可能在新使用者在安裝擴充程式再繼續測試)。
a)預設使用者的擴充:
在鎖定chrome的工作列開啟的狀態:
b) WebDriver開啟的新使用者的擴充:
在鎖定chrome的工作列開啟的狀態:
-----------------------------------------------------------------本文------------------------------------------------------------------------
版本說明:Selenium 2.0
Chrome: 34.0.1847.116 m
系統:Windows 7 x86
解決辦法:
//設定Webdriver啟動chrome為預設使用者的配置資訊(包括書籤、擴充程式等)ChromeOptions options = new ChromeOptions();options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");WebDriver driver = new ChromeDriver(options);
注意:上例中user_name為 你電腦的使用者名稱。
ps:網上搜的答案好多是 在路徑 User_Data又添加了一個default,但經過我測試是開啟的chrome仍然是新使用者。
附參考代碼:
說明:
(1)請保證chromedriver路徑正確;
(2) status.html 中不走代理的話 js:src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" 是無法載入成功;js載入成功,則頁面的radio為選中狀態。
(3)保證代理設定正常。
Browser_Chrome.java
package test;import org.testng.annotations.Test;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;public class Browser_Chrome {@Testpublic void test() throws InterruptedException{System.setProperty("webdriver.chrome.driver", "D:/SETEST/selenium/chromedriver.exe");//設定Webdriver啟動chrome為預設使用者的配置資訊(包括書籤、擴充程式等)ChromeOptions options = new ChromeOptions();options.addArguments("user-data-dir=C:/Users/test/AppData/Local/Google/Chrome/User Data");WebDriver driver = new ChromeDriver(options);driver.get("D:/SETEST/selenium/status-test.html");WebElement radio = driver.findElement(By.name("radio"));((JavascriptExecutor)driver).executeScript("$('#radio').click();");System.out.println(radio.isSelected());Thread.sleep(2000);//driver.close();//driver.quit();}}
status-test.html
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>status</title> <script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" /> <body><h3>status</h3><div class="row-fluid"><div class="span3"> <input name="user" placeholder="Disabled TextField" disabled /> </div> <div class="span3"><a class="btn disabled">Disabled Button</a></div><div class="span3"><input name="radio" type="radio" id="radio" /><input name="display" style="display:none" /></div></div> </body><script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script></html>
運行(注意擴充程式 和 radio的選中狀態——F12開發人員工具——可以直接存取js指令碼)
--------------------------------------------------------------------------------------------------------------------------------------------------------
附開啟新使用者chrome載入失敗的code和:
Browser_Chrome.java(載入失敗的code)
package test;import org.testng.annotations.Test;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class Browser_Chrome {@Testpublic void test() throws InterruptedException{System.setProperty("webdriver.chrome.driver", "D:/SETEST/selenium/chromedriver.exe");WebDriver driver = new ChromeDriver();driver.get("D:/SETEST/selenium/status-test.html");WebElement radio = driver.findElement(By.name("radio"));((JavascriptExecutor)driver).executeScript("$('#radio').click();");System.out.println(radio.isSelected());Thread.sleep(2000);//driver.close();//driver.quit();}}
運行(載入失敗,js開啟為空白):
參考:連結1
設定Webdriver啟動chrome為預設使用者的配置資訊