学习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())
相关推荐
hongyucai44 分钟前
随意学习-模型工程:因子、隐空间、采样策略
学习
Amazing_Cacao5 小时前
CFCA精品可可产区风土体系(亚洲):彻底拒绝应试背诵,将感官复核作为检验真实物理差异的唯一铁律
学习
tkevinjd5 小时前
MiniCode 项目详解6:原项目控制系统的10个缺陷(已修复)
python·llm·agent
dNGUZ7UGj7 小时前
10分钟完成第一个Python小游戏
python·django
没有梦想的咸鱼185-1037-16637 小时前
AI-Python机器学习与深度学习技术:CNN/Transformer/扩散模型、SHAP可解释及Hermes智能体自动化
人工智能·python·深度学习·机器学习·chatgpt·cnn·transformer
霸道流氓气质7 小时前
SpringBoot中通用工具类库(Utils)封装与使用实践
spring boot·后端·python
Draina9 小时前
CBC填充预言攻击-CBC Padding Oracle Crypto Attack
python·安全·web安全·网络安全·密码学·安全性测试
Angel Q.9 小时前
特征提取 | DINO 到 DINOv3
图像处理·python
不如语冰10 小时前
AI大模型入门-pytorch-张量2 tensor创建
python
NPE~10 小时前
[AI]Agent开发——ADK框架使用
人工智能·python·ai·教程·adk·agent开发