2017-01-28 131 views
2

我想向我的selenium webdriver添加python請求會話cookie。Selenium從CookieJar添加Cookie

我已經試過這到目前爲止

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. 有cookiejar的Firefox和Chrome的任何特殊迭代?
  2. 爲什麼它在PhantomJS上工作?
+0

誰?請 – ProFan

回答

2
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