一尘不染

无法使用Selenium和Splinter添加Cookie

selenium

对于下面的内容,我不确定如何解决这种非常简单的使用情况。

我有脚本功能上做了相同的事情,这(我跑大约每月一次) 工作作为一个月前。

我会很高兴有任何地方的指针开始研究为什么它不起作用。

$ python3
Python 3.6.1 (default, Mar 23 2017, 16:49:06)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> from splinter import Browser
>>> chrome_options = webdriver.ChromeOptions()
>>> browser = Browser('chrome')
>>> browser.cookies.add({'aaa':'bbb'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dummyuser/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/splinter/driver/webdriver/cookie_manager.py", line 28, in add
    self.driver.add_cookie({'name': key, 'value': value})
  File "/Users/dummyuser/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 708, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "/Users/dummyuser/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/Users/dummyuser/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unable to set cookie
  (Session info: chrome=62.0.3202.94)
  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.13.1 x86_64)

阅读 513

收藏
2020-06-26

共1个答案

一尘不染

您应该先打开url并加载cookie,然后再打开包含cookie的下一个URL。如果要打开相同的url,也可以像这样打开:

driver = webdriver.Chrome(executable_path=r'X:\home\xxx\chromedriver.exe')

cookies = pickle.load(open("cookies.pkl", "rb"))
driver.get("https://www.douban.com/")
for cookie in cookies:
    driver.add_cookie(cookie)
driver.get("https://www.douban.com/")

希望这可以帮助

2020-06-26