shobha kotagiri
shobha kotagiri

Reputation: 39

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

I am able to add extension(.crx ) through selenium web driver

ChromeOptions options = new ChromeOptions();

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

But unable to add name and value through selenium java. can anyone help me on this?

Upvotes: 2

Views: 12823

Answers (4)

Prakhya
Prakhya

Reputation: 21

In my case I have downloaded the "chrome-modheader" floder from https://github.com/modheader/modheader_selenium and then put it in our project path and used the below snippet to create chrome options and use it when the driver instance is created. It works with the existing modheaders.

Below code for Selenium-Java-Chrome

Path currentRelativePath = Paths.get("my localpath/chrome-modheader/modheader.crx"); ChromeOptions options = new ChromeOptions(); options.addExtensions(new File(currentRelativePath.toAbsolutePath().toString())); ChromeDriver driver = new ChromeDriver(options); driver.get("https://webdriver.modheader.com/add?headerName=value"); new WebDriverWait(driver, 30).until(ExpectedConditions.titleIs("Done"));

Upvotes: 0

ankit
ankit

Reputation: 5127

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

Upvotes: 0

Asif Choudhury
Asif Choudhury

Reputation: 1

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

Upvotes: 0

Florent B.
Florent B.

Reputation: 42528

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

Upvotes: 2

Related Questions