无法使用Selenium&添加cookie.碎片 [英] Unable to add a cookie using Selenium & Splinter

查看:126
本文介绍了无法使用Selenium&添加cookie.碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我不确定如何解决这种非常简单的使用情况.

Per below, I'm not sure how to troubleshoot this pretty simple usage scenario.

我有一个脚本(我大约每月运行一次),该脚本在功能上执行相同的操作,并在一个月前使用了 .

I have script (that I run about once a month) that functionally does the identical thing and which used to work as of a month ago.

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

I'd appreciate any pointers on places to start looking into why this does not work.

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

推荐答案

您应该先打开url,然后加载cookie,然后再打开包含cookie的下一个URL.如果要打开相同的网址:

you should open the url first, and load the cookies, then you can open the next url with cookies.you can also open like that if you want open the same 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/")

希望这会有所帮助

这篇关于无法使用Selenium&amp;添加cookie.碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆