chrome modify headers in selenium java, i am able to add extension .crx through script

那年仲夏 提交于 2019-12-04 16:13:19

Chrome stores the settings of an extension in the localstorage. So one way to customize your extension is to first set the context on it and then edit the localstorage with a piece of Javascript.

Here is an example adding two headers (token1 and token2) to ModHeader:

// 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/");
Asif Choudhury

Download 2.1.2-Crx4Chrome.com.crx and try this below code

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: []                                                     " +
"}]));

One way to do it, is to replicate user behavior

Open extension option page and fill details you want to fill in.

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")

Open your desired url now.

driver.get("http://google.com")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!