从CookieJar中添加Selenium Cookies

8
我将尝试为我的Selenium WebDriver添加Python请求会话cookies。
到目前为止,我已经尝试了以下方法:
for c in self.s.cookies :
    driver.add_cookie({'name': c.name, 'value': c.value, 'path': c.path, 'expiry': c.expires})

这段代码在PhantomJS上运行良好,但在Firefox和Chrome上却不行。

我的问题:

  1. Firefox和Chrome的cookiejar需要特殊迭代吗?
  2. 为什么它在PhantomJS上可以工作?
1个回答

8
for cookie in s.cookies:  # session cookies
    # Setting domain to None automatically instructs most webdrivers to use the domain of the current window
    # handle
    cookie_dict = {'domain': None, 'name': cookie.name, 'value': cookie.value, 'secure': cookie.secure}
    if cookie.expires:
        cookie_dict['expiry'] = cookie.expires
    if cookie.path_specified:
        cookie_dict['path'] = cookie.path

    driver.add_cookie(cookie_dict)

请查看以下链接,获取完整的解决方案:https://github.com/cryzed/Selenium-Requests/blob/master/seleniumrequests/request.py


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接