Selenium 和 PyAutoGUI 实现UI自动化测试

阶段一:环境准备与基础操作

1.1 安装工具库

bash 复制代码
# 安装Selenium(用于Web自动化)
pip install selenium

# 安装PyAutoGUI(用于桌面应用自动化)
pip install pyautogui

1.2 浏览器驱动下载

  • 下载对应浏览器版本的驱动(如ChromeDriver)
  • 将驱动文件放在Python安装目录或系统PATH路径中

阶段二:Selenium基础操作

2.1 启动浏览器并打开网页

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By

# 启动Chrome浏览器
driver = webdriver.Chrome()

# 打开网页
driver.get("https://www.baidu.com")

# 关闭浏览器
driver.quit()

2.2 元素定位与操作

python 复制代码
# 通过ID定位搜索框并输入内容
search_box = driver.find_element(By.ID, "kw")
search_box.send_keys("Python自动化测试")

# 通过NAME定位搜索按钮并点击
search_button = driver.find_element(By.NAME, "wd")
search_button.click()

# 通过XPath定位元素
element = driver.find_element(By.XPATH, "//input[@class='s_ipt']")

阶段三:进阶操作

3.1 处理弹窗和窗口切换

python 复制代码
# 切换到弹窗
alert = driver.switch_to.alert
alert.accept()  # 确认弹窗

# 切换窗口
for handle in driver.window_handles:
    driver.switch_to.window(handle)

3.2 显式等待(解决元素加载问题)

python 复制代码
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 等待元素出现(最多10秒)
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "element_id"))
)

阶段四:实际案例 - 电商网站自动化测试

4.1 模拟登录流程

python 复制代码
def test_login():
    driver.get("https://www.example.com/login")
    driver.find_element(By.ID, "username").send_keys("test_user")
    driver.find_element(By.ID, "password").send_keys("password123")
    driver.find_element(By.XPATH, "//button[text()='登录']").click()
    assert "我的账户" in driver.title

4.2 模拟购物流程

python 复制代码
def test_shopping():
    # 搜索商品
    driver.find_element(By.NAME, "q").send_keys("手机")
    driver.find_element(By.CLASS_NAME, "search-btn").click()
    
    # 添加第一个商品到购物车
    driver.find_element(By.XPATH, "(//a[contains(text(),'加入购物车')])[1]").click()
    
    # 进入购物车结算
    driver.find_element(By.LINK_TEXT, "去结算").click()
    assert "订单确认" in driver.page_source

阶段五:PyAutoGUI桌面自动化

5.1 基础鼠标键盘操作

python 复制代码
import pyautogui

# 移动鼠标并点击
pyautogui.moveTo(100, 200, duration=1)  # 移动到(100,200)位置,耗时1秒
pyautogui.click()  # 左键单击

# 输入文本
pyautogui.typewrite("Hello, PyAutoGUI!", interval=0.1)  # 逐个字符输入

# 快捷键操作
pyautogui.hotkey('ctrl', 'c')  # 模拟Ctrl+C

5.2 图像识别点击

python 复制代码
# 在屏幕上查找指定图片并点击
button_location = pyautogui.locateOnScreen('button.png')
if button_location:
    pyautogui.click(button_location)

阶段六:最佳实践与调试

6.1 使用Page Object模式(提高代码复用性)

python 复制代码
# 示例:登录页面对象化
class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_field = (By.ID, "username")
        self.password_field = (By.ID, "password")
        self.login_button = (By.XPATH, "//button[text()='登录']")

    def login(self, username, password):
        self.driver.find_element(*self.username_field).send_keys(username)
        self.driver.find_element(*self.password_field).send_keys(password)
        self.driver.find_element(*self.login_button).click()

6.2 错误处理与截图

python 复制代码
try:
    driver.find_element(By.ID, "non-existent-element").click()
except NoSuchElementException:
    driver.save_screenshot("error.png")  # 保存错误截图

阶段七:扩展学习方向

  1. Appium:移动端自动化测试

  2. PyTest:测试框架集成

  3. Headless模式 :无界面浏览器测试

    python 复制代码
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    driver = webdriver.Chrome(options=options)
相关推荐
李昊哲小课15 分钟前
销售数据可视化分析项目
python·信息可视化·数据分析·matplotlib·数据可视化·seaborn
烛阴25 分钟前
带参数的Python装饰器原来这么简单,5分钟彻底掌握!
前端·python
全干engineer1 小时前
Flask 入门教程:用 Python 快速搭建你的第一个 Web 应用
后端·python·flask·web
nightunderblackcat1 小时前
新手向:Python网络编程,搭建简易HTTP服务器
网络·python·http
李昊哲小课1 小时前
pandas销售数据分析
人工智能·python·数据挖掘·数据分析·pandas
C嘎嘎嵌入式开发1 小时前
python之set详谈
开发语言·python
之歆2 小时前
Python-正则表达式-信息提取-滑动窗口-数据分发-文件加载及分析器-浏览器分析-学习笔记
python·学习·正则表达式
往日情怀酿做酒 V17639296382 小时前
pytorch的介绍以及张量的创建
人工智能·pytorch·python
豌豆花下猫3 小时前
Python 潮流周刊#110:JIT 编译器两年回顾,AI 智能体工具大爆发(摘要)
后端·python·ai
June bug3 小时前
【Python基础】变量、运算与内存管理全解析
开发语言·python·职场和发展·测试