PPT
代码地址
课后调查表
页面暂停
mock 数据 Faker
知识点回顾-xmind思维导图
2023Selenium 框架.xmind (213.0 KB)
from time import sleep
import yaml
from selenium import webdriver
class TestCookieLogin:
def setup_class(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(3)
def teardown_class(self):
self.driver.quit()
def test_get_cookies(self):
self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
sleep(20)
cookies = self.driver.get_cookies()
with open("cookie.yaml","w") as f:
yaml.safe_dump(cookies,f)
def test_add_cookies(self):
self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
cookies = yaml.safe_load(open("cookie.yaml"))
for c in cookies:
self.driver.add_cookie(c)
self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
import time
import yaml
from selenium import webdriver
class TestCookie:
# 页面预处理
def setup_class(self):
self.driver = webdriver.Chrome()
self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx?from=myhome_baidu")
# 数据收尾
def teardown_class(self):
self.driver.quit()
def test_save_cookie(self):
# 手动扫码,获取cookie
time.sleep(30)
# 获取cookie,返回字典
cookie = self.driver.get_cookies()
# 保存cookie
with open('./cookie.yaml','w',encoding='u8') as f:
# yaml支持字典,python字典解析为yaml字典
yaml.safe_dump(cookie,f)
def test_add_cookie(self):
# 从文件获取cookie,yaml字典转为python字典
cookie = yaml.safe_load(open('./cookie.yaml','r',encoding='u8'))
# 遍历每条cookie
for i in cookie:
# 单条cookie加入到driver中
self.driver.add_cookie(i)
# 使用带cookie的driver访问
self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx?from=myhome_baidu")