Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "invalid argument: invalid 'expiry'" in Selenium when adding cookies to a chromedriver?

I'm trying to add cookies to a browser, but getting the following error:

Message: invalid argument: invalid 'expiry' (Session info: chrome=75.0.3770.90)

This code shows how I load and add cookies.

for cookie in pickle.load(open(r'{0}\{1}_cookie.pkl'.format(settings.COOKIES_PATH, self.tv_username), 'rb')):
    self.browser.add_cookie(cookie)

Values of the cookies:

{'__utmc': '226258911', '_sp_id.cf1a': '0b243b32-8dee-46d9-a243-bb4d2bfcb805.1560942815.1.1560942821.1560942815.5941cbc0-0500-4a17-8f6c-4ee3f133f67c', 'km_vs': '1', '__utmt': '1', '__utma': '226258911.510671571.1560942814.1560942814.1560942814.1', 'km_ai': 'm6caeIAUtEqx%2BIWda%2F7klZER%2F1Y%3D', 'kvcd': '1560942821112', '__utmb': '226258911.2.10.1560942814', 'km_lv': '1560942821', '__utmz': '226258911.1560942814.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)', 'etg': 'undefined', '_sp_ses.cf1a':'*'}

It worked fine with previous version of chrome. Any help would be appreciated!

like image 925
andreilozhkin Avatar asked Jun 19 '19 08:06

andreilozhkin


3 Answers

This is caused by an active bug in chromedriver: https://bugs.chromium.org/p/chromedriver/issues/detail?id=3331

The bug is that chromedriver returns expiry cookies with get_cookies as a double, but does not accept a double for it with add_cookie. Here's the fix:

for cookie in pickle.load(open('cookies.pkl', 'rb')):
    expiry = cookie.get('expiry', None)

    if expiry:
      cookie['expiry'] = int(expiry * 1000)
    browser.add_cookie(cookie)
like image 133
jcady Avatar answered Sep 17 '22 12:09

jcady


In my case, the previous answer did not work. I had to remove the expire key from the object.

 for cookie in pickle.load(open(PATH, "rb")):
     if 'expiry' in cookie:
         del cookie['expiry']

     self.driver.add_cookie(cookie)

This happens if you previously pickled the cookies directly as they're returned from the driver, like so:

pickle.dump(browser.get_cookies(), open(PATH, "wb"))
like image 41
Yoannes Geissler Avatar answered Sep 21 '22 12:09

Yoannes Geissler


In my version of python and selenium, I have found that on there is a difference between how Selenium outputs cookie expiry values and how it imports them. When you use

driver.get_cookies()

the driver can output expiry values that are floats rather than integers. These floats seem to be epoch time units (number of seconds since Jan 1, 1970). If you try to add these exact cookies back into driver, they will fail because the driver only accepts cookies with integer expiry values. In this line:

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

the value of expiry MUST be an integer. Otherwise, you will get the value error. I fixed this using the following code.

# Saving current cookies and reformatting them
cookies = driver.get_cookies()
for cookie in cookies:
    if 'expiry' in cookie:
        cookie['expiry'] = int(cookie['expiry'])
    # Adding cookies back into the driver
    driver.add_cookie(cookie)

This worked for me, and I no longer get an error.

like image 21
Scott Driggers Avatar answered Sep 17 '22 12:09

Scott Driggers