2015-12-21 45 views
2

本質上我有一個程序,利用請求庫和它的發佈請求。我想在發送請求被調用後將會話的cookie加載到webdriver中。我正在考慮使用硒和鉻二元體系,但我對如何解決這個問題感到困惑。導出要求cookies到網絡驅動器

基本上我有這麼多。

import requests 
url=www.storeUrl.com 
session=requests.Session() 
data={'utf8': '%E2%9C%93', 
    'auth': 'oBLLJkW+A2plgT1lUJeKXq7DdqJSniGnZhnSmpuAQOE%3D', 
    'stat': 'null', 
    'user': 'zdw7287'} 
session.cookies.clear() 
response=session.post(url,data=data) 
storeResponse=session.request('get','http://www.storeUrl.com') 

print storeResponse.cookies 

類 'requests.cookies.RequestsCookieJar'>曲奇_store_session = BAh7CUkiD3Nlc3Npb25faWQGOgZFRkkiJTBiYmY4MmEzNmRmMjZkMjNhZDdiODg4NWVmYWQ5Y2IzBjsAVEkiB3RqBjsARnsLSSIHcDAGOwBGSXU6CVRpbWUNte4cgFPoSgUKOgtAX3pvbmVJIghFU1QGOwBUOg1uYW5vX251bWkCGgE6DW5hbm9fZGVuaQY6DXN1Ym1pY3JvIgcoIDoLb2Zmc2V0af6wuUkiB3AxBjsARjBJIgdwMgY7AEYwSSIHY3MGOwBGMEkiB2NjBjsARjBJIghpcHMGOwBGWwYiETI0LjkxLjIyNi4zNkkiCWNhcnQGOwBGewdpAph7aQY6C2Nvb2tpZUkiHTEgaXRlbS0tJDM2LS0zMTY0MCwxMjY0MwY7AFRJIhBfY3NyZl90b2tlbgY7AEZJIjFxNHI4QWFUQWNWaXZmY2xIVlNPcHRQeUk2ODF2NTVhbm9pREE1YWFSOHpNPQY7AEY%3D - eea073c1f0a4fd19163e39536e75eed04ab788f9爲www.storeUrl.com />]>

我該如何去裝載這個cookie到硒?任何幫助將不勝感激。

回答

2

Selenium已建成add_cookie方法添加餅乾當前會話:

In [4]: browser.add_cookie?? 
Type:  instancemethod 
String form: <bound method WebDriver.add_cookie of <selenium.webdriver.chrome.webdriver.WebDriver (session="8b6e7ab963b91b5050fa1b8ef2bd7bfa")>> 
File:  /usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py 
Definition: browser.add_cookie(self, cookie_dict) 
Source: 
    def add_cookie(self, cookie_dict): 
     """ 
     Adds a cookie to your current session. 

     :Args: 
     - cookie_dict: A dictionary object, with required keys - "name" and "value"; 
      optional keys - "path", "domain", "secure", "expiry" 

     Usage: 
      driver.add_cookie({'name' : 'foo', 'value' : 'bar'}) 
      driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/'}) 
      driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure':True}) 

     """ 
     self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict}) 

基本上,你需要用餅乾dict傳遞給add_cookie方法和session.cookies.get_dict()回報dict與餅乾:

driver.add_cookie(session.cookies.get_dict())


SO例子: