selenium4.x 之POM概况

Page Object Mpde

一、基类

basePage

复制代码
import logging
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
logger = logging.getLogger("PO")


class BasePage:
    """poge的基类"""

    def __init__(self, driver: webdriver.Chrome):
        self._driver = driver
        self._wait = WebDriverWait(driver, 10)   # 自动等待
        logger.info("PO实例化成功")

    def __getattr__(self, item):
        key = f"_loc_{item}"
        if hasattr(self, key):
            xpath = getattr(self, key)
            return self.get_element(xpath)
        raise AttributeError(f'元素不存在:{item}')

    def get_element(self, xpath):
        logger.info("正在进行元素定位")
        el = self._wait.until(lambda x: self._driver.find_element(By.XPATH, xpath))
        logger.info("元素定位成功")
        return el

    # alert弹窗
    def alert(self):
        time.sleep()

        # alert = driver.switch_to.alert
        def _a():
            return self._driver.switch_to.alert  # 可能会失败,可能会成功

        alert = self._wait.until(_a)
        alert.accept()
    
    # 系统信息
    def get_msg(self):
        time.sleep(0.5)  # 等待-0.5秒
        el = self._driver.find_element(By.XPATH, "//p[@class='prompt-msg']")
        return el.text

Python--getattr反射+selenium详解篇_selenium kwargs.get-CSDN博客

二、页面

page:可以讲属性和方法分开

属性(元素)和方法(元素的操作)

复制代码
class LoginPage(BasePage):
    _loc_code = ('//div/ul/li[@class="account-tab-account"]')
    _loc_username = ('//input[@id="username"]')
    _loc_password = ('//input[@id="password"]')

    def login(self, username, password):
        self.code.click()
        self.username.send_keys(username)
        self.password.send_keys(password)

元素可单独封装

三、测试用例

复制代码
def test_00():
    driver = get_webdriver()
    driver.get("https://accounts.douban.com/passport/login")
    page = LoginPage(driver)
    page.login("12255", "123456")
相关推荐
jiuweiC3 分钟前
python 虚拟环境-windows
开发语言·windows·python
free-elcmacom6 分钟前
机器学习入门<5>支持向量机形象教学:寻找最安全的“三八线”,人人都能懂的算法核心
人工智能·python·算法·机器学习·支持向量机
月亮!19 分钟前
人工智能发展现状与未来
人工智能·python·selenium·测试工具·开源·测试用例
天草二十六_简村人23 分钟前
jenkins打包制作Python镜像,并推送至docker仓库,部署到k8s
后端·python·docker·容器·kubernetes·jenkins
weixin_4577600025 分钟前
GIOU (Generalized Intersection over Union) 详解
pytorch·python
汐ya~25 分钟前
Cursor连接异常问题“Model not available/connection failed”解决:IDE内置代理配置,无需全局流量代理与虚拟网卡
python·编辑器·cursor
海市公约31 分钟前
Python操作SQLite数据库:从基础语法到完整项目实战
数据库·ide·python·程序人生·架构·pycharm·sqlite
轻竹办公PPT31 分钟前
学校要求开题报告 PPT,有没有模板?
人工智能·python·powerpoint
神奇的代码在哪里1 小时前
C++的演进与我的编程学习之旅:从底层基础到AI应用
c++·人工智能·python·学习·程序人生·个人开发
lanbo_ai1 小时前
基于深度学习的宠物猫品种识别系统,resnet50,alexnet,mobilenet【pytorch框架,python代码】
人工智能·pytorch·python·深度学习·cnn