Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

python - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...