20220703 web 实战训练营

PPT

代码地址

思维导图

2022selenium.xmind.zip (6.3 KB)

实战练习一

  • 植入 cookie 登录(参见【Cookie 复用】)

实战练习二

  • 添加通讯录成员
  • 首页点击添加成员
  • 输入姓名 帐号,手机号
  • 点击保存
  • 验证添加成功
#实现代码
import yaml
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestLogin:
    def setup_class(self):
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        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')#使用手动扫码登录
        cookies=self.driver.get_cookies()
        # 获取cookie并保存
        with open("./qw_cookies",mode='w',encoding="utf-8")as f:
            yaml.safe_dump(cookies,f)

    def test_add_contact(self):#获取并设置cookie,完成通讯录点击操作
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        with open("./qw_cookies")as f:
            cookies_data=yaml.safe_load(f)
        for cookie in cookies_data:
            self.driver.add_cookie(cookie)
        self.driver.find_elements(By.CSS_SELECTOR, ".frame_nav_item_title")[2].click()
1 个赞
from time import sleep
import yaml
from selenium import webdriver


class TestLogin:
    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)
        self.driver.maximize_window()

    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(30)
        cookie = self.driver.get_cookies()
        with open('../cookies/cookie.yaml', 'w', encoding='u8') as f:
            yaml.safe_dump(cookie, f)

    def test_add_cookies(self):
        self.driver.get('https://work.weixin.qq.com/wework_admin/loginpage_wx')
        with open('../cookies/cookie.yaml', 'r', encoding='u8') as f:
            cookies = yaml.safe_load(f)
        for cookie in cookies:
            self.driver.add_cookie(cookie)
            sleep(3)
        self.driver.get('https://work.weixin.qq.com/wework_admin/loginpage_wx')

1 个赞
import json
from time import sleep

from selenium import webdriver


def get_cookies():
    driver = webdriver.Chrome()
    driver.get('https://work.weixin.qq.com/wework_admin/frame')
    sleep(20)
    with open('cookies.json', 'w') as f:
        f.write(json.dumps(driver.get_cookies()))


def login():
    driver = webdriver.Chrome()
    driver.delete_all_cookies()
    driver.get('https://work.weixin.qq.com/wework_admin/frame')
    with open('cookies.json', 'r') as f:
        cookie_list = json.load(f)
        for cookie in cookie_list:
            driver.add_cookie(cookie)
    driver.refresh()
    sleep(10)

if __name__ == '__main__':
    login()

1 个赞

    def test_add_cookie(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        cookie = yaml.safe_load(open("../cookies.yaml"))
        for c in cookie:
            self.driver.add_cookie(c)
        sleep(3)

        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        # 首页点击添加成员
        self.driver.find_element(By.XPATH, '//*[text()="添加成员"]').click()
        # 输入相关信息
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys('sig')
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_acctid").send_keys('rid')
        self.driver.find_element(By.CSS_SELECTOR, '#memberAdd_phone').send_keys('15811111112')
        # 点击保存
        self.driver.find_element(By.CSS_SELECTOR, '.qui_btn.ww_btn.js_btn_save').click()
        # 获取列表信息
        res = self.driver.find_elements(By.XPATH, '//*[text()="sig"]')
        # 断言列表中是否为空,不为空则添加成功
        assert res != []
1 个赞
from time import sleep
import yaml
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class TestLogin:
    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)
        self.driver.maximize_window()
        self.driver.get('https://work.weixin.qq.com/wework_admin/loginpage_wx')
        with open('../cookies/cookie.yaml', 'r', encoding='u8') as f:
            cookies = yaml.safe_load(f)
        for cookie in cookies:
            self.driver.add_cookie(cookie)
        self.driver.get('https://work.weixin.qq.com/wework_admin/loginpage_wx')

    def teardown_class(self):
        # self.driver.quit()
        pass

    def test_get_cookies(self):
        self.driver.get('https://work.weixin.qq.com/wework_admin/loginpage_wx')
        sleep(30)
        cookie = self.driver.get_cookies()
        with open('../cookies/cookie.yaml', 'w', encoding='u8') as f:
            yaml.safe_dump(cookie, f)

    def test_add_member(self):
        self.driver.find_element('xpath', '//span[text()="添加成员"]').click()
        self.driver.find_element('css selector', '#username').send_keys('TestEngineer1')
        self.driver.find_element('css selector', '#memberAdd_acctid').send_keys('TESTID_001')
        self.driver.find_element('css selector', '#memberAdd_phone').send_keys('13100000001')
        self.driver.find_element('link text', '保存').click()
        WebDriverWait(self.driver, 10, 0.5).until(
            expected_conditions.visibility_of_element_located(('css selector', '#js_tips')))
        res = self.driver.find_element('css selector', '#js_tips').text
        assert res == '保存成功'

课后作业1: 从通讯录页面:添加成员

from time import sleep

import yaml
from faker import Faker
from selenium import webdriver
from selenium.common import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions


class TestAddMember:
    def setup_class(self):
        # 初始化drvier的浏览器
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)

        # 复用cookies登陆
        self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
        with open('cookies.yaml', 'r') as f:
            cookie_list = yaml.safe_load(f)
            for cookie in cookie_list:
                self.driver.add_cookie(cookie)
        # 刷新页面
        self.driver.refresh()

        # 随机生成姓名、账号、手机号
        fake = Faker(locale='zh-CN')
        self.name = fake.name()
        self.num = fake.postcode()  # 账号
        self.phone_number = fake.phone_number()

    def teardown_class(self):
        self.driver.quit()

    def test_login(self):
        pass

    def test_add_member(self):
        # 点击通讯录
        self.driver.find_element(By.CSS_SELECTOR, "#menu_contacts>.frame_nav_item_title").click()

        # 1.点击 添加成员 按钮 ,这里元素查找过快,页面还未刷新导致的StaleElementReferenceException异常
        # 尝试点击3次,每次失败等待2s
        for i in range(3):
            try:  # 尝试寻找 添加按钮并点击
                self.driver.find_element(By.CSS_SELECTOR, '.qui_btn.ww_btn.js_add_member:nth-child(2)').click()
            except StaleElementReferenceException:  # 抛出异常就,等待2s,跳过循环的后面代码,执行下一次循环查找元素
                sleep(2)
                continue
            else:  # 如果,没有异常结束循环
                break
                
        # 2.输入姓名、账号、邮箱号、手机号
        self.driver.find_element(By.ID, 'username').send_keys(self.name)
        self.driver.find_element(By.ID, 'memberAdd_acctid').send_keys(self.num)
        self.driver.find_element(By.ID, 'memberAdd_biz_mail').send_keys(self.phone_number)
        self.driver.find_element(By.ID, 'memberAdd_phone').send_keys(self.phone_number)
        # 3、点击保存
        # 注意当css选择器中有空格时,需要用点 . 代理
        self.driver.find_element(By.CSS_SELECTOR, '.qui_btn.ww_btn.js_btn_save').click()
        # 4、验证是否成功,验证最后出现的那个保存成功的  弹框
        element = WebDriverWait(self.driver, 10, 0.5).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, '#js_tips'))
        )
        assert element.text == '保存成功'

1 个赞