2

Essentially I have a program that makes use of the requests library and its post request. I want to take the cookies of the session after the post request has been called and load them into a webdriver. I was thinking of making use of selenium and a chrome binary but I am confused on how to go about it.

Basically what I have this far.

import requests
url=www.storeUrl.com
session=requests.Session()
data={'utf8': '%E2%9C%93',
  'auth': 'oBLLJkW+A2plgT1lUJeKXq7DdqJSniGnZhnSmpuAQOE%3D',
  'stat': 'null',
  'user': 'zdw7287'}
session.cookies.clear()
response=session.post(url,data=data)
storeResponse=session.request('get','http://www.storeUrl.com')

print storeResponse.cookies

class 'requests.cookies.RequestsCookieJar'>Cookie _store_session=BAh7CUkiD3Nlc3Npb25faWQGOgZFRkkiJTBiYmY4MmEzNmRmMjZkMjNhZDdiODg4NWVmYWQ5Y2IzBjsAVEkiB3RqBjsARnsLSSIHcDAGOwBGSXU6CVRpbWUNte4cgFPoSgUKOgtAX3pvbmVJIghFU1QGOwBUOg1uYW5vX251bWkCGgE6DW5hbm9fZGVuaQY6DXN1Ym1pY3JvIgcoIDoLb2Zmc2V0af6wuUkiB3AxBjsARjBJIgdwMgY7AEYwSSIHY3MGOwBGMEkiB2NjBjsARjBJIghpcHMGOwBGWwYiETI0LjkxLjIyNi4zNkkiCWNhcnQGOwBGewdpAph7aQY6C2Nvb2tpZUkiHTEgaXRlbS0tJDM2LS0zMTY0MCwxMjY0MwY7AFRJIhBfY3NyZl90b2tlbgY7AEZJIjFxNHI4QWFUQWNWaXZmY2xIVlNPcHRQeUk2ODF2NTVhbm9pREE1YWFSOHpNPQY7AEY%3D--eea073c1f0a4fd19163e39536e75eed04ab788f9 for www.storeUrl.com/>]>

How would I go about loading this cookie into selenium? Any help would be greatly appreciated.

Thomas Dukes
  • 139
  • 1
  • 2
  • 10

2 Answers2

3

Selenium has built in add_cookie method for adding cookies to current session:

In [4]: browser.add_cookie??
Type:        instancemethod
String form: <bound method WebDriver.add_cookie of <selenium.webdriver.chrome.webdriver.WebDriver (session="8b6e7ab963b91b5050fa1b8ef2bd7bfa")>>
File:        /usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py
Definition:  browser.add_cookie(self, cookie_dict)
Source:
    def add_cookie(self, cookie_dict):
        """
        Adds a cookie to your current session.

        :Args:
         - cookie_dict: A dictionary object, with required keys - "name" and "value";
            optional keys - "path", "domain", "secure", "expiry"

        Usage:
            driver.add_cookie({'name' : 'foo', 'value' : 'bar'})
            driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/'})
            driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure':True})

        """
        self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})

Basically, you need to pass a dict with cookies to add_cookie method and session.cookies.get_dict() returns dict with cookies:

driver.add_cookie(session.cookies.get_dict())


SO examples:

Community
  • 1
  • 1
drets
  • 2,583
  • 2
  • 24
  • 38
  • 4
    ```self.driver.add_cookie(self.cookies.get_dict()) *** selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'name' (Session info: chrome=73.0.3683.75) (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Mac OS X 10.15.0 x86_64) (``` – Ammad Khalid Oct 23 '19 at 18:04
0
from selenium import webdriver
driver = webdriver.Firefox()

cookeis = storeResponse.cookies # cookiejar like in question 

driver.get('<your url>') # catch InvalidCookieDomainException without in

for key in cookies.keys():
    # print({name: key, value: cookies[key]}) # just print
    driver.add_cookie({'name': key, 'value': cookies[key]})