学习selenium+python使用 XPath 表达式来实现找到目标元素时智能封装等待,执行测试代码启动Chrome浏览器后,地址栏只显示data;

背景

  1. 学习使用 XPath 表达式来实现找到目标元素时智能封装等待
  2. 执行测试代码启动Chrome浏览器后,地址栏只显示data;

代码如下

python 复制代码
import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from HTMLTestRunner_cn import HTMLTestRunner

class MyTest(unittest.TestCase):

    def setUp(self) -> None:
        chrome_option = webdriver.ChromeOptions()
        chrome_option.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(options=chrome_option)
        self.imgs = []  # 初始化存放测试截图的列表
        self.url = 'http://localhost:8080'

    def tearDown(self) -> None:
        try:
            self.driver.quit()
        except NoSuchElementException as e:
            print('tearDown Error details: {}'.format(e.args[0]))

    def find_element(self, locator):
        try:
            element = WebDriverWait(self.driver, 30).until(lambda x: x.find_element(*locator))
            return element
        except NoSuchElementException as e:
            print('Error details: {}'.format(e.args[0]))
            raise

    def test1(self):
        self.find_element(('id', 'username')).send_keys('admin')
        self.find_element(('id', 'password')).send_keys('admin')
        self.find_element(('xpath', '//input[@value="Login"]')).click()
        # 执行截图操作,将当前截图加入到测试报告中
        self.imgs.append(self.driver.get_screenshot_as_base64())
        self.find_element(('xpath', '//div[@id="accordion"]//div[contains(@class, "panel-title") and text()="信息查询"]')).click()
        self.imgs.append(self.driver.get_screenshot_as_base64())
        self.find_element(('partial link text', '查询顾客信息')).click()
        self.imgs.append(self.driver.get_screenshot_as_base64())

if __name__ == '__main__':
    test1 = unittest.defaultTestLoader.loadTestsFromTestCase(MyTest)
    suite = unittest.TestSuite(test1)

    # unittest.TextTestRunner().run(suite)

    runner = HTMLTestRunner(
        title='带截图的测试报告',
        description='xxx软件测试报告v0.1',
        stream=open('reports/sample_test_report.html', 'wb'),
        verbosity=2
    )

    runner.run(suite)

解决过程

  1. 看了好久找到了替代启动浏览器的方法:换成self.driver.get('http://localhost:8080')就好了
  2. 然后开始琢磨两者的区别:
    • 使用 self.driver.get('http://localhost:8080') 时,driver 是一个 WebDriver 对象,通过调用 get() 方法并传入网址参数来打开浏览器,并加载对应的网页。
    • 而当使用 self.url = 'http://localhost:8080' 时,你只是将网址赋值给了 self.url 这个实例变量,但并没有使用它来打开浏览器。所以在后续的代码中,浏览器仍然会使用默认的网址或者之前通过 driver.get() 方法设置的网址。
    • 如果希望使用 self.url 来打开浏览器,可以在测试方法 test1() 中,通过 self.driver.get(self.url) 来打开指定的网页。
python 复制代码
def test1(self):
    self.driver.get(self.url)
    self.find_element(('id', 'username')).send_keys('admin')
    self.find_element(('id', 'password')).send_keys('admin')
    self.find_element(('xpath', '//input[@value="Login"]')).click()
    # 执行截图操作,将当前截图加入到测试报告中
    self.imgs.append(self.driver.get_screenshot_as_base64())
    self.find_element(('xpath', '//div[@id="accordion"]//div[contains(@class, "panel-title") and text()="信息查询"]')).click()
    self.imgs.append(self.driver.get_screenshot_as_base64())
    self.find_element(('partial link text', '查询顾客信息')).click()
    self.imgs.append(self.driver.get_screenshot_as_base64())
相关推荐
AOI小白新手上路1 小时前
DRSformer·论文蒸馏笔记:可学习 Top-k 稀疏注意力去雨网络
网络·笔记·学习
weixin199701080161 小时前
《淘宝TOP API:落地方案、接口边界与业务踩坑 —— 聚石塔内外价差 10 倍的真相》(附 Python 源码)
开发语言·python
梦想的颜色1 小时前
【编程实战】AI 时代 APP 开发全栈硬核指南:技术选型 + AI 架构 + 模型落地全维度决策
python·flutter·react native·react.js·ai·桌面应用·milvus
Madison-No72 小时前
多语言聊天大模型--测试报告
linux·git·python·selenium·jmeter·自动化·postman
鲲鹏ai2 小时前
盈启鲲鹏数字人招商政策
大数据·人工智能·python
荣码2 小时前
从0到1搭一个生产级RAG系统:串联前面21篇所有知识
java·python
m4Rk_2 小时前
【论文阅读】Agent 记忆机制(96):M3-Agent——让 Agent 从视听经历中持续形成情景与语义长期记忆
论文阅读·人工智能·学习·开源·github
小师兄吃牛肉3 小时前
Java语法 | 多重循环
java·开发语言·python
明如正午3 小时前
用 Python 一键把 ASC 文件转成 BLF(基于 python-can)
python·can·blf·asc
~光~~3 小时前
【嵌入式linux学习】Cache 策略:Write-Through / Write-Back + Allocation
linux·学习·spring