1

I have a question about selenium webdriver for python. I'm creating an instance of Chrome to login into a website, then I would like to create multiple instances of webdriver USING the same first instance (like a copy) or at least multiple instances which keep stored credentials that I used.

simple code :

options = webdriver.ChromeOptions()
options.binary_location = "path/to/chrome.exe"

browser1 = webdriver.Chrome(chrome_options=options)
browser1.get(login_url)

browser1.find_element_by_name('auth-email').send_keys('@gmail.com')
browser1.find_element_by_name('auth-password').send_keys('pass' + Keys.RETURN)

Example :

I logged into https://github.com/ with browser1. If I open a new instance browser2 and browser3 and I get url like https://github.com/pulls which needs to be logged to see contents, I have to RE-LOGIN again to see content.

Is there a way to copy credentials from browser1 to browser2 and browser3 ? Thanks in advance and sorry for my english,

1 Answer 1

3

Selenium does not offer a method to "clone" a new Selenium instance from an other one.

One thing you can do is to extract the authentication cookies from the first browser and pass them to the new browsers. For instance, to save the value of the sessionid cookie:

sessionid = driver.get_cookie("sessionid")["value"]

and set it:

driver.add_cookie({'name': 'sessionid',
                   'value': sessionid})

I use this kind of code to avoid having to login for each test in a large test suite. You must fetch a page from the web site first before you can set cookies for that site.

There's a caveat, however. This won't generally work if the cookie is marked HTTP-only. This is set by the web site. When a cookie is HTTP-only, it cannot be accessed by JavaScript code that run in web pages. A while back, I found that Selenium in fact was able to access HTTP-only cookies in FF and Chrome but not in IE. I do not know whether this inconsistent behavior has changed but for my tests I turned of the HTTP-only flag. (In production, it is turned on.)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your answer, however I can't make it work .. I tried every cookies and still i can't get the page without having to re-log again. Is it possible to copy ALL cookies at one time from the first browser and add it to the others ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.