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

后端 未结 4 774
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 10:14

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

相关标签:
4条回答
  • 2020-12-15 10:25

    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)
    
    0 讨论(0)
  • 2020-12-15 10:29

    The problem is that you are trying to add the cookies with a different format than the selenium expects.

    The python selenium api reference says that you have to insert the cookies with a dict like that

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

    So you have to adapt your loop to use a key,value format

    for key, value in pickle.load(open(r'{0}\{1}_cookie.pkl'.format(settings.COOKIES_PATH, self.tv_username), 'rb')):
        self.browser.add_cookie({'name' : key, 'value' : value})
    
    0 讨论(0)
  • 2020-12-15 10:29

    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"))
    
    0 讨论(0)
  • 2020-12-15 10:44

    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.

    0 讨论(0)
提交回复
热议问题