Selenium3自动化测试31Cookie处理

少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述Selenium3自动化测试31Cookie处理相关的知识,希望能为你提供帮助。
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来解决问题。
#仅仅启动浏览器后,观察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
【Selenium3自动化测试31Cookie处理】#仅仅启动浏览器后,观察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()
br/>sleep(2)
driver.find_element_by_xpath("//*[@id=login-wechat]/div[3]/a").click()
" )
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【测试全系列视频课程】请点击我哦… ( https://edu.51cto.com/lecturer/968349.html) 学习路线如下 ![微信图片_20210816100905.png](https://s2.51cto.com/images/20210816/1629079635804582.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)![微信图片_202108161009051.png](https://s2.51cto.com/images/20210816/1629079635990692.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)![微信图片_202108161009052.png](https://s2.51cto.com/images/20210816/1629079635138091.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 图书京东、当当有售 京东: https://item.jd.com/12784287.html 当当: http://product.dangdang.com/29177828.html ![0.9733767060093851.png](https://s2.51cto.com/images/20210816/1629079289752959.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)如果你觉的文章读的不过瘾,可以查看详细的视频教程。 【2021】UI自动化测试:Selenium3自动化测试 https://edu.51cto.com/course/26488.html【测试全系列视频课程】请点击我哦… ( https://edu.51cto.com/lecturer/968349.html) 学习路线如下 ![微信图片_20210816100905.png](https://s2.51cto.com/images/20210816/1629079635804582.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)![微信图片_202108161009051.png](https://s2.51cto.com/images/20210816/1629079635990692.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)![微信图片_202108161009052.png](https://s2.51cto.com/images/20210816/1629079635138091.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 图书京东、当当有售 京东: https://item.jd.com/12784287.html 当当: http://product.dangdang.com/29177828.html ![0.9733767060093851.png](https://s2.51cto.com/images/20210816/1629079289752959.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)


    推荐阅读