selenium設置chrome瀏覽器保持登陸方式兩種options和cookie

方法一:html

1.在初始化driver以前先設置optionspython

2.其餘關於options介紹連接:http://www.cnblogs.com/baoyu7yi/p/7058537.htmlweb

3.代碼chrome

from selenium import webdriver #實例化谷歌設置選項
option = webdriver.ChromeOptions() #添加保持登陸的數據路徑:安裝目錄通常在C:\Users\黃\AppData\Local\Google\Chrome\User Data
option.add_argument(r"user-data-dir=C:\Users\黃\AppData\Local\Google\Chrome\User Data") #初始化driver
driver = webdriver.Chrome(options=option) driver.get("https://ke.qq.com/") driver.maximize_window()

 方法二:瀏覽器

1.經過F12查找到保存登陸的cookie服務器

2.cookie添加的源碼解讀cookie

  由源碼能夠看出,添加的cookie是字典類型的。添加鍵值爲name和value,還有其餘可選的鍵。session

 這些鍵名都表明什麼意思呢?參考博文《selenium3.7+ python3 添加cookie模擬登錄》,羅列以下dom

  • name:cookie的名稱網站

  • value:cookie對應的值,動態生成的

  • domain:服務器域名

  • expiry:Cookie有效終止日期

  • path:Path屬性定義了Web服務器上哪些路徑下的頁面可獲取服務器設置的Cookie

  • httpOnly:防腳本攻擊

  • secure:在Cookie中標記該變量,代表只有當瀏覽器和Web Server之間的通訊協議爲加密認證協議時

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

3.webdriver提供了一些操做cookie的方法。
1)get_cookies():獲取全部的cookie信息
2)get_cookie(name):返回字典key爲「name」的cookie信息
3)add_cookie(cookie_dict):添加cookie,「cookie_dict」指的是字典對象,必須有key和value
4)delete_cookie(name,optionsString):刪除cookie信息。name爲要刪除的cookie名稱,optionsString指該cookie的選項,目前支持的選項包括」路徑「和」域「
5)delete_all_cookies():刪除全部cookie信息。

4.例:添加一個cookie,保持登陸

from selenium import webdriver ''' 1.F12獲取cookie,在添加cookie先後都要寫上driver.get() 2.此處cookie獲取就一個 ''' driver = webdriver.Chrome() cookie1 = {"name":"_redmine_session","value":"SzByNTJSUXBReEViVUJZd1lLRll4bnFJMW1rRHdiTFVtOXM1ck85M0tiMXB6bWRXOUxoc21leTlWVlFSQ2tHRHVrZS90dTQ0T0pZZmxjQ0dQakRNR2FuMS9keVBXWmdSMEQ3TFJ0T0JHVWlHNEsxMWVZRkdpRFR5amxLbVl5NDZaeWhDRGpHM2M4TzN5VHM4ZTZ4ckV2VWVCbTgrVEFDdVU4YjVOcDNmcHNqYUVjN082YllkUm95YzR4V2dJRkNtZVhTVkVXUHFpRmRXOFRFcXppZHl5UUw5Z1NYUEZYRWRLdTBjZVc3RU5yYz0tLXgzcmcxL2c4ODIzN2hRWnV2QVl3cEE9PQ%3D%3D--3af0df2799ffc8552ef1d9dbf7f5e90379a2ecbd"} driver.get("http://192.168.129.109/projects/ptts-1-0?jump=account") driver.add_cookie(cookie_dict=cookie1) driver.get("http://192.168.129.109/projects/ptts-1-0?jump=account")

5.例:添加兩個cookie,保持登陸狀態(以百度頁面登陸爲例)

driver1 = webdriver.Chrome() driver1.maximize_window() cookie2 = {"name":"BDUSS","value":"UJJTH5rcE9qd1VsWDZ5SzFsQkJVZUVPZTMwRi05bzg4UDE5YXJYZDAzNklkRzViQVFBQUFBJCQAAAAAAAAAAAEAAADzdT9HTGlseTAyNTE4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIjnRluI50Zbe"} cookie3 = {"name":"BAIDUID","value":"F513A1C2ADA0A19C868F711C1792A3D0"} driver1.get("https://passport.baidu.com/center") #將獲取的兩個cookie都添加上
driver1.add_cookie(cookie2) driver1.add_cookie(cookie3) driver1.get("https://passport.baidu.com/center")

 

6.在輸入如下代碼會出現報錯爲:Message: unable to set cookie。報錯信息以下

 

Traceback (most recent call last): File "D:/python_workshop/python6/selenium_webdriver/add_cookies的使用(二).py", line 9, in <module> driver.add_cookie(cookie_dict=cookies) File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 872, in add_cookie self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict}) File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute self.error_handler.check_response(response) File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unable to set cookie (Session info: chrome=66.0.3359.139) (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7600 x86_64)

 

 解決方法:參考文檔--https://www.cnblogs.com/CoolJayson/p/7421013.html

                須要在driver.add_cookie()前加上driver1.get("url地址")。

               必須首先加載網站,這樣Selenium 才能知道cookie 屬於哪一個網站,即便加載網站的行爲對咱們沒任何用處。以下代碼加粗部分。

driver1 = webdriver.Chrome() driver1.maximize_window() cookie2 = {"name":"BDUSS","value":"UJJTH5rcE9qd1VsWDZ5SzFsQkJVZUVPZTMwRi05bzg4UDE5YXJYZDAzNklkRzViQVFBQUFBJCQAAAAAAAAAAAEAAADzdT9HTGlseTAyNTE4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIjnRluI50Zbe"} cookie3 = {"name":"BAIDUID","value":"F513A1C2ADA0A19C868F711C1792A3D0"} driver1.get("https://passport.baidu.com/center") #將獲取的兩個cookie都添加上
driver1.add_cookie(cookie2)
driver1.add_cookie(cookie3) driver1.get("https://passport.baidu.com/center")
相關文章
相關標籤/搜索