設定Webdriver啟動chrome為預設使用者的配置資訊

來源:互聯網
上載者:User

標籤: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為預設使用者的配置資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.