selenium-pytest基础使用,利用cookie欺骗实现免密登录,最大化、最小化窗口,利用js获取页面的加载时间,取出cookie、添加cookie值
1、利用cookie欺骗实现免密登录简书
import json from selenium import webdriver from time import sleep import pytest class Test_Jianshu(object): def setup_class(self): '''打开浏览器''' self.driver = webdriver.Chrome() self.driver.get("https://www.jianshu.com/") self.driver.implicitly_wait(2) def test_getcookie(self): '''获取cookie''' cookie = self.driver.get_cookies() self.driver.find_element_by_id("sign_in").click() sleep(2) self.driver.find_element_by_id("weixin").click() '''延迟20s,扫码登录''' sleep(20) cookie = self.driver.get_cookies() '''将cookie转成json''' jsonCookies = json.dumps(cookie) '''将cookie值存得到文件中''' with open('./a.txt', 'w') as f: f.write(jsonCookies) def test_login(self): '''加载文件中的cookie''' with open('./a.txt', 'r', encoding='utf8') as f: listCookies = json.loads(f.read()) '''浏览器加载cookie''' for cookie in listCookies: cookie_dict = { 'domain': '.jianshu.com', 'name': cookie.get('name'), 'value': cookie.get('value'), "expires": '', 'path': '/', 'httpOnly': False, 'HostOnly': False, 'Secure': False } self.driver.add_cookie(cookie_dict) # 刷新浏览器 self.driver.refresh() def teardown_class(self): '''关闭浏览器''' self.driver.quit()
2、其他使用
1)最大化、最小化窗口
2)利用js获取页面的加载时间
3)取出cookie、添加cookie值
import json from selenium import webdriver import pytest from time import sleep import allure # from selenium.webdriver import DesiredCapabilities class Test_one(object): def setup_class(self): allure.step('打开浏览器') self.driver = webdriver.Chrome() # self.driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.CHROME) self.driver.get("https://xueqiu.com") @pytest.mark.skip def test_basic(self): allure.step("最大化窗口") self.driver.get("https://xueqiu.com") sleep(1) self.driver.maximize_window() allure.step("将窗口全屏") self.driver.fullscreen_window() @pytest.mark.skip def test_execute_js(self): allure.story('利用js获取页面的加载时间') raw = self.driver.execute_script("return JSON.stringify(window.performance.timing)") allure.step('格式化') print(json.dumps(json.loads(raw),indent=4)) def test_cookie(self): allure.step('取出cookie') print(self.driver.get_cookies()) allure.step('新增cookie') self.driver.add_cookie({'name':'name_a','value':'value_a'}) print(self.driver.get_cookies()) def teardown_class(self): self.driver.quit() print('关闭浏览器')
3、testerhome.com注册模块自动化、登录数据参数化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from selenium import webdriver import pytest import time import allure @pytest.fixture(scope="module") def openBrower(): global driver driver = webdriver.Chrome() driver.get("https://testerhome.com/teams") time.sleep(2) driver.find_element_by_link_text("求职面试圈").click() time.sleep(2) driver.find_element_by_xpath("//div[@data-prefetch='/topics/27641']").click() time.sleep(2) yield driver.quit() data=[["yangchun123","yanghcun","yangchun@sina.com","123456"],["zs123","zs","zs@sina.com","123456"]] @pytest.mark.parametrize("user_login,name,email,pwd",data) @pytest.mark.usefixtures("openBrower") def test_click(user_login,name,email,pwd): allure.story('注册') driver.find_element_by_xpath("//a[@href='/account/sign_up']").click() time.sleep(3) allure.step('填写注册信息') driver.find_element_by_id('user_login').send_keys(user_login) driver.find_element_by_id('user_name').send_keys(name) driver.find_element_by_id('user_email').send_keys(email) driver.find_element_by_id('user_password').send_keys(pwd) driver.find_element_by_id('user_password_confirmation').send_keys(pwd) driver.find_element_by_xpath("//input[@type='submit']").click() time.sleep(3)