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)
T
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.