天天看點

Selenium3自動化測試【31】Cookie處理

實際過程中,大家經常聽到Cookie與Session。其中Cookie是放在浏覽器端(用戶端);Session放在伺服器端,每個客戶在伺服器端都有與其對應的Session。
在網站中,http請求是無狀态的。簡單來說即第一次和伺服器連接配接且登入成功後,第二次請求伺服器依然不能知道目前請求是哪個使用者。而Cookie的出現就是為了解決該問題,第一次登入後伺服器傳回一些資料(Cookie)給浏覽器,然後浏覽器會将其儲存在本地,當該使用者發送第二次請求的時候,就會自動的把上次請求存儲的Cookie資料自動的傳遞給伺服器,伺服器通過浏覽器攜帶的資料就能判斷目前使用者是哪個了。
Cookie存儲的資料量有限,不同的浏覽器有不同的存儲大小,一般不超過4KB。是以Cookie隻能存儲一些小量的資料。
Cookie是由網絡伺服器發送出來存儲在網絡浏覽器上,下次同一客戶再次通路該伺服器時,可從該浏覽器讀取此資訊。Cookie讓浏覽器記住這位訪客的特定資訊,如上次通路的位置等。
Cookie 是個存儲在浏覽器目錄的文本檔案,當浏覽器運作時,存儲在 RAM 中。一旦你從該網站或網絡伺服器退出,Cookie 也可存儲在計算機的硬碟上。當訪客結束其浏覽器對話時,即終止所有 Cookie。
Cookie可以保持登入資訊到使用者下次與伺服器的會話,換句話說,下次通路同一網站時,使用者會發現不必輸入username和pwd就已經登入了。但有一些Cookie在使用者退出會話的時候就被删除了(使用者也可手工删除本地Cookie),這樣可以有效保護個人隐私。
Cookie在生成時會被指定一個Expire值,這就是Cookie的生存周期,在這個周期内Cookie有效,超出周期Cookie就會被清除。有些頁面将Cookie的生存周期設定為“0”或負值,這樣在關閉浏覽器時,就馬上清除Cookie,不會記錄使用者資訊,更加安全。

Cookie的屬性

一般Cookie所具有的屬性,包括:

**1. Domain:域 **

Domain:域,表示目前Cookie所屬于哪個域或子域下面;對于伺服器傳回的Set-Cookie中,如果沒有指定Domain的值,那麼其Domain的值是預設為目前所送出的http的請求所對應的主域名。比如通路 http://www.example.com,傳回一個cookie,沒有指名domain值,那麼其值為預設的www.example.com;

2. Path

Path:表示Cookie的所屬路徑;

3. Expire time/Max-age

Expire time/Max-age:表示了Cookie的有效期。expire的值,是一個時間,過了這個時間,該Cookie就失效了。或者是用max-age指定目前Cookie是在多長時間之後而失效。如果伺服器傳回的一個Cookie,沒有指定其expire time,那麼表明此Cookie有效期隻是目前的Session,即是session cookie,目前Session會話結束後,就過期了。對應的,當關閉(浏覽器中)該頁面的時候,此cookie就被浏覽器删除了;

4.secure

secure:表示該Cookie隻能用https傳輸。一般用于包含認證資訊的Cookie,要求傳輸此cookie的時候,必須用https傳輸;

5.httponly

httponly:表示此Cookie必須用于http或https傳輸。這意味着,浏覽器腳本(比如Javascript中)是不允許通路操作Cookie。

對于一些需要輸入驗證碼才能登陸的網站,可以采用Cookie來解決問題。

  1. 擷取Cookies的方法:get_cookies();
  2. 擷取指定name的Cookie:driver.get_cookie(name);
  3. 清除Cookie:delete_cookie()。

擷取cookie

1.未登入網站

打開浏覽器,而不輸入任何網址,檢視Cookie。

from selenium import webdriver
from time import sleep

#僅僅啟動浏覽器後,觀察cookies
driver = webdriver.Firefox()
print("僅僅啟動浏覽器後的cookies == %s" % driver.get_cookies())
driver.quit()
           

結果:Cookies值為空

僅僅啟動浏覽器後的cookies == []

2.擷取Cookie(僅僅打開網站)

打開浏覽器,打開51cto的登入頁,再獲得cookies。

from selenium import webdriver
from time import sleep

#僅僅啟動浏覽器後,觀察cookies
driver = webdriver.Firefox()
print("僅僅啟動浏覽器後的cookies == %s" % driver.get_cookies())
sleep(2)
driver.get("https://home.51cto.com/index?reback=http://www.51cto.com/")
#啟動啟動浏覽器後,打開51cto網站,觀察cookies
print("打開51cto網站後的cookies == %s" % driver.get_cookies())
sleep(2)
driver.quit()
           

運作結果,觀察到Cookie的内容如下:

打開51cto網站後的
cookies == [{'name': 'waf_cookie', 'value': '95e69c9e-fb7a-4f25c8a6a40e1e70de0d13a1aa4db5c91dac', 'path': '/', 'domain': 'home.51cto.com', 'secure': False, 'httpOnly': True}, {'name': 'acw_tc', 'value': '2760826615830716737054425e572528b6ee0a77b6c7f775925c4cc8da5e38', 'path': '/', 'domain': 'home.51cto.com', 'secure': False, 'httpOnly': True, 'expiry': 1585750075}, {'name': 'PHPSESSID', 'value': 'g6pfj5a2fsk8mluifre3affm65', 'path': '/', 'domain': 'home.51cto.com', 'secure': False, 'httpOnly': True}, {'name': '_csrf', 'value': 'eab71d25bbb517398da316a7767175c406ee473aa513645b039a9d544c87b4b7a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22BMUMbCHkLJaFBgJ7ZWq7RRCqqi1HhqH6%22%3B%7D', 'path': '/', 'domain': 'home.51cto.com', 'secure': False, 'httpOnly': True}, {'name': '_ourplusFirstTime', 'value': '120-3-1-22-7-54', 'path': '/', 'domain': 'home.51cto.com', 'secure': False, 'httpOnly': False, 'expiry': 1619071674}, {'name': '_ourplusReturnTime', 'value': '120-3-1-22-7-54', 'path': '/', 'domain': 'home.51cto.com', 'secure': False, 'httpOnly': False, 'expiry': 1619071674}, {'name': '_ourplusReturnCount', 'value': '1', 'path': '/', 'domain': 'home.51cto.com', 'secure': False, 'httpOnly': False, 'expiry': 1619071674}, {'name': 'www51cto', 'value': '50DD08DE4D45347E6D9C324988918495Xnbn', 'path': '/', 'domain': '.51cto.com', 'secure': False, 'httpOnly': False, 'expiry': 1898431674}, {'name': 'Hm_lvt_844390da7774b6a92b34d40f8e16f5ac', 'value': '1583071675', 'path': '/', 'domain': '.home.51cto.com', 'secure': False, 'httpOnly': False, 'expiry': 1614607675}, {'name': 'Hm_lpvt_844390da7774b6a92b34d40f8e16f5ac', 'value': '1583071675', 'path': '/', 'domain': '.home.51cto.com', 'secure': False, 'httpOnly': False}]
           

3.獲得Cookie(登入後)

先登入51cto網站,在獲得Cookies,運作代碼,觀察到輸出的Cookie有了變化。

from selenium import webdriver
from time import sleep

#僅僅啟動浏覽器後,觀察cookies
driver = webdriver.Firefox()
print("僅僅啟動浏覽器後的cookies == %s" % driver.get_cookies())
sleep(2)
driver.get("https://home.51cto.com/index?reback=http://www.51cto.com/")
#啟動浏覽器後,打開51cto網站,觀察cookies
print("打開51cto網站後的cookies == %s" % driver.get_cookies())
sleep(2)
driver.find_element_by_xpath("//*[@id='login-wechat']/div[3]/a").click()
driver.find_element_by_xpath("//*[@id='loginform-username']").send_keys("hb****")
driver.find_element_by_xpath('//*[@id="loginform-password"]').send_keys("87654321")
# driver.find_element_by_id("loginform-password").send_keys("87654321")  #定義pwd,通過id也可的哦
driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/input[1]').click()
sleep(2)
print("登入51cto網站後的cookies == %s" % driver.get_cookies())
sleep(2)
driver.quit()
           

結果:

登入51cto網站後的
cookies == [{'name': 'www51cto', 'value': '50DD08DE4D45347E6D9C324988918495Xnbn', 'path': '/', 'domain': '.51cto.com', 'secure': False, 'httpOnly': False, 'expiry': 1898431674}, {'name': 'pub_sauth1', 'value': 'CQZVHBVWBVM6DQQJUA1fPQUGDFdWBQQHVVo', 'path': '/', 'domain': '.51cto.com', 'secure': False, 'httpOnly': True, 'expiry': 1583935680}, {'name': 'pub_sauth2', 'value': '83976623bdbfba728859ba24480e9d78', 'path': '/', 'domain': '.51cto.com', 'secure': False, 'httpOnly': True, 'expiry': 1583935680}, {'name': 'pub_cookietime', 'value': '864000', 'path': '/', 'domain': '.51cto.com', 'secure': False, 'httpOnly': True, 'expiry': 1583935680}, {'name': 'pub_wechatopen', 'value': 'aG0wVVBdBFIEBQQGWg', 'path': '/', 'domain': '.51cto.com', 'secure': False, 'httpOnly': True, 'expiry': 1585663680}, {'name': 'pub_sauth3', 'value': 'UgQMAFQJVARRWgVWBAMEUwpVUFoAAlpWAlcAWlEHAwMAAl0OVggHVGxQBwFTBVdQBgReDFdSBwEHUwYNAVVUU1FVXQZVWwEFV1sDV1EFUAdWPlcNW1JSAgBTAQA', 'path': '/', 'domain': '.51cto.com', 'secure': False, 'httpOnly': True, 'expiry': 1583935680}, {'name': 'waf_cookie', 'value': 'b6ffca6f-f59f-40e7621bd3c842b5b5f0d84d41de6d6065d6', 'path': '/', 'domain': 'www.51cto.com', 'secure': False, 'httpOnly': True}, {'name': 'acw_tc', 'value': '2760828515830716798753340e43a84ecf598bb85eaeea74247db3f9f4488e', 'path': '/', 'domain': 'www.51cto.com', 'secure': False, 'httpOnly': True, 'expiry': 1585750081}]
           

--------------------------------------------------------

:::

如果你覺的文章讀的不過瘾,可以檢視詳細的視訊教程。

【2021】UI自動化測試:Selenium3自動化測試

 https://edu.51cto.com/course/26488.html

繼續閱讀