Selenium从CookieJar添加Cookie

时间:2017-01-28 05:38:36

标签: python selenium

我正在尝试将python请求会话cookie添加到我的selenium webdriver。

到目前为止我已尝试过这个

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 个答案:

答案 0 :(得分:5)

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