How to add new request header to chrome webdriver in robot framework

1.4k Views Asked by At

I need to add a new request header to chrome webdriver. In manual test, we are using ModHeader extension to add new custom request header to the browser.

I tried following option, but did not work.

${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver\
Call Method       ${options}       add_argument        newheadername\=newheadervalue\
Create Webdriver    Chrome    chrome_options=${options}\
Go To    ${URL}

How do I launch the web application with a custom request header?

Can we add the customer request header to chrome options and create webdriver with the options?

3

There are 3 best solutions below

2
Steve Rawlins On

What is newheadervalue? Did you mean to supply a variable there such as ${newheadervalue}?

   ${chrome_options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver

Call Method    ${chrome_options}   add_argument    start-maximized
Call Method    ${chrome_options}   add_argument    newheadername=newheadervalue
Call Method    ${chrome options}   add_argument    window-size=1920,1080

${options}=     Call Method     ${chrome_options}    to_capabilities     
Open Browser     ${HOST_URL}    browser=chrome  desired_capabilities=${options}
0
Govi On

Header name is x-iteration and a value has to be passed. For now, I have hardcoded the value.

I tried the following code in keyword, but still I do not see the 'x-iteration' header added in the launched browser request headers

[Arguments] ${URL}
${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome_options} add_argument x-iteration=itr-dec-w2
${options}= Call Method ${chrome_options} to_capabilities
Open Browser ${URL} browser=chrome desired_capabilities=${options}

0
ajain On

I used chrome extension modheader to achieve this like below.

Open Available Browser
...    about:blank
...    browser_selection=chrome
...    options=add_extension('modheader.crx');add_experimental_option('prefs',{"extensions.ui.developer_mode": True,})
Go To    https://webdriver.modheader.com/add?test=ModHeader%20Test
Go To    YourWebsite

Modheader has the selenium support which is explained here https://docs.modheader.com/advanced/selenium-webdriver