chrome修改了selenium java中的头文件,我可以通过脚本添加扩展名.crx

时间:2016-05-26 09:29:00

标签: selenium-chromedriver

我可以通过selenium web driver

添加扩展名(.crx)
ChromeOptions options = new ChromeOptions();

options.addExtensions(new File("src/main/resources/idgpnmonknjnojddfkpgkljpfnnfcklj.crx"));

但无法通过selenium java添加名称和值。有人可以帮我吗?

3 个答案:

答案 0 :(得分:0)

Chrome会将扩展程序的设置存储在localstorage中。因此,自定义扩展的一种方法是首先在其上设置上下文,然后使用一段Javascript编辑localstorage。

以下是向ModHeader添加两个标头(token1和token2)的示例:

// add the ModHeader extension
ChromeOptions  options = new ChromeOptions();
options.addExtensions(new File("C:\\Downloads\\ModHeader_v2.0.9.crx"));

// launch the browser
WebDriver driver = new ChromeDriver(options);

// set the context on the extension so the localStorage can be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");

// setup ModHeader with two headers (token1 and token2)
((JavascriptExecutor)driver).executeScript(
    "localStorage.setItem('profiles', JSON.stringify([{                " +
    "  title: 'Selenium', hideComment: true, appendMode: '',           " +
    "  headers: [                                                      " +
    "    {enabled: true, name: 'token1', value: '01234', comment: ''}, " +
    "    {enabled: true, name: 'token2', value: '56789', comment: ''}  " +
    "  ],                                                              " +
    "  respHeaders: [],                                                " +
    "  filters: []                                                     " +
    "}]));                                                             " );

// visit a page
driver.get("http://stackoverflow.com/");

答案 1 :(得分:0)

下载2.1.2-Crx4Chrome.com.crx并尝试以下代码

String userAgent = PropertyReader.readItem("USER-AGENT");
String xmsisdn = PropertyReader.readItem("X-MSISDN");
String xUPSUBNO = PropertyReader.readItem("xUPSUBNO");

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(PropertyReader.readItem("CHROMEEXT")));

options.addArguments("--start-maximized");
options.addArguments("disable-infobars");

System.setProperty("webdriver.chrome.driver","Path\\chromedriver.exe");

//Launch the Browser 

webDriver = new ChromeDriver(options); 

// set the context on the extension so the localStorage can be accessed 

webDriver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");

// setup ModHeader with name and value
JavascriptExecutor js;
((JavascriptExecutor)webDriver).executeScript(
"localStorage.setItem('profiles', JSON.stringify([{                " +
"  title: 'Selenium', hideComment: true, appendMode: '',           " +
"  headers: [                                                      " +
"    {enabled: true, name: 'User-Agent', value: '"+userAgent+"', comment: ''}, " +
"    {enabled: true, name: 'X-UP-SUBNO', value: '"+xUPSUBNO+"', comment: ''}  " +
"  ],                                                              " +
"  respHeaders: [],                                                " +
"  filters: []                                                     " +
"}]));

答案 2 :(得分:0)

一种方法是复制用户行为

打开扩展程序选项页面并填写要填写的详细信息。

driver.get("chrome-extension://innpjfdalfhpcoinfnehdnbkglpmogdi/options.html")
by_id("btn_start")
by_id("btn_add_new")
select = Select(driver.find_element_by_id('action_1'))
select.select_by_visible_text('Add')
send_by_name('name','name')
send_by_name('value','value')
by_id("btn_save_1")
by_id("btn_enable_1")

立即打开所需的网址。

driver.get("http://google.com")