dunstorm
dunstorm

Reputation: 392

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?

Upvotes: 9

Views: 9851

Answers (1)

Bart
Bart

Reputation: 543

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

Upvotes: 9

Related Questions