Selenium遇到Exception自动截图

随手小记

场景:测试百度:

点击新闻,跳转到新的窗口,找到输入框,输入"hello,world"

等到输入框的内容是hello,world, 这里有个错误,少了一个]

后来就实现了错误截图的功能,可以参考

复制代码
//input[@id='ww'"

 WebDriverWait(chrome_driver,10).until(expected_conditions.text_to_be_present_in_element_value((By.XPATH,"//input[@id='ww'"),"hello,world"))

完整代码

复制代码
# 通过xpath定位,By.XPATH
# xpath是一门在xml文档中查找信息的语言
# 为什么使用xpath可以定位htmk
# html文档的结构和标签嵌套遵循XML的基本规则,因此XPath可以有效的用于html文档中定位元素。XPath
# 从而实现定位
# xpath定位html的基本原则是,将html文档视为xml文档,并且使用Xpath表达式来描述元素的位置关系
# 选择特定的元素或者元素集合

# xpath可以通过相对路径和绝对路径去定位元素,绝对路径从html根节点开始算,相对路径从任意节点开始
import time
import os

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

chrome_driver = webdriver.Chrome()
try:
    # 1.从根节点开始选取:绝对路径
    chrome_driver.get("https://www.baidu.com")
    chrome_driver.maximize_window()
    # 类变量  绝对路径
    # chrome_driver.find_element(By.XPATH,)
    chrome_driver.implicitly_wait(10)
    chrome_driver.find_element(By.XPATH,'/html[1]/body[1]/div[1]/div[1]/div[3]/a[1]').click()
    # 切换窗口
    all_window = chrome_driver.window_handles
    chrome_driver.switch_to.window(all_window[1])
    # driver.findElement(By.xpath("//img[@alt='百度新闻']"))
    WebDriverWait(chrome_driver,10).until(expected_conditions.presence_of_element_located((By.XPATH,"//img[@alt='百度新闻']")))
    assert 'news' in chrome_driver.current_url
    print(chrome_driver.current_url)
    time.sleep(5)

    # 从任意节点开始取,//div 列出所有的div标签
    web_eles = chrome_driver.find_elements(By.XPATH,'//div')
    print(web_eles)
    print(len(web_eles))
    # 这个网站取练手杠杠滴
    # https://iviewui.com/view-ui-plus/component/form/form
    #选取当前父节点是input的下面的a元素
    web_a = chrome_driver.find_elements(By.XPATH,'//div/a')
    print(len(web_a))

    # 根据属性选取
    # 1.使用id属性定位
    chrome_driver.find_element(By.XPATH,"//input[@id='ww']").send_keys("hello,world")
    WebDriverWait(chrome_driver,10).until(expected_conditions.text_to_be_present_in_element_value((By.XPATH,"//input[@id='ww']"),"hello,world"))
    chrome_driver.find_element(By.XPATH,"//input[@id='ww']").clear()
    time.sleep(5)

except Exception as e:
    # 截图存储的文件夹
    screenshot_dir = os.path.join(os.getcwd(), "screenshot")
    # 如果目录不存在,则创建
    if not os.path.exists(screenshot_dir):
        os.makedirs(screenshot_dir)
    # 获取当前截图目录下所有的 PNG 文件
    existing_screenshots = [f for f in os.listdir(screenshot_dir) if f.endswith(".png")]
    # 计算新的截图编号
    new_index = len(existing_screenshots) + 1  # 从1开始累加
    # 生成新的截图路径
    screenshot_path = os.path.join(screenshot_dir, f"{new_index}.png")
    # 发生异常时截图
    chrome_driver.save_screenshot(screenshot_path)
    print(f"操作失败,已截图: {screenshot_path}")
    print(f"错误信息: {e}")

finally:
    # 关闭浏览器
    chrome_driver.quit()

最后的截图

相关推荐
程序员小远13 天前
自动化测试基础知识总结
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
LT101579744413 天前
2026年Web自动化测试工具选型指南:多浏览器兼容解决方案
前端·测试工具·自动化
程序员三藏14 天前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
深蓝电商API14 天前
Selenium 5.0 全新架构解析:值得升级吗?
爬虫·selenium
专业机床数据采集14 天前
基于 Wireshark 抓包逆向设备通信协议,并用 C# UDP协议跨平台 实现宝元数控程序列表读取、上传、下载和删除
网络·测试工具·wireshark·程序传输·宝元数控·dnc·数控程序传输
程序员龙叔14 天前
从 0 开始学习 AI 测试 - 从接口测试来教你如何用 AI 来生成自动化测试代码
自动化测试·软件测试·python·软件测试工程师·测试工具·性能测试·ai测试
Saniffer_SH14 天前
【高清视频】Gen6 服务器还没到,Gen6 SSD 怎么测?Emily 现场演示三种测试环境
人工智能·驱动开发·测试工具·缓存·fpga开发·计算机外设·压力测试
川石课堂软件测试14 天前
APP自动化测试|高级手势操作&toast操作
css·功能测试·测试工具·microsoft·fiddler·单元测试·harmonyos
LT101579744414 天前
2026年开源自动化测试工具选型指南:功能与适用场景解析
测试工具·开源·自动化