Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Add Cookies From CookieJar

I am trying to add python requests session cookies to my selenium webdriver.

I have tried this so far

for c in self.s.cookies :
    driver.add_cookie({'name': c.name, 'value': c.value, 'path': c.path, 'expiry': c.expires})

This code is working fine for PhantomJS whereas it's not for Firefox and Chrome.

My Questions:

  1. Is there any special iterating of cookiejar for Firefox and Chrome?
  2. Why it is working for PhantomJS?
like image 778
dunstorm Avatar asked Jan 28 '17 05:01

dunstorm


People also ask

Where we can store cookies in Selenium?

When the code is executed, webdriver will store the cookie information using FileWriter Class to write streams of characters and BufferedWriter to write the text into a file named “Cookiefile. data“. The file stores cookie information – “Name, Value, Domain, Path”.


1 Answers

for cookie in s.cookies:  # session cookies
    # Setting domain to None automatically instructs most webdrivers to use the domain of the current window
    # handle
    cookie_dict = {'domain': None, 'name': cookie.name, 'value': cookie.value, 'secure': cookie.secure}
    if cookie.expires:
        cookie_dict['expiry'] = cookie.expires
    if cookie.path_specified:
        cookie_dict['path'] = cookie.path

    driver.add_cookie(cookie_dict)

Check this for a complete solution https://github.com/cryzed/Selenium-Requests/blob/master/seleniumrequests/request.py

like image 148
Bart Avatar answered Sep 27 '22 22:09

Bart