当前位置: 首页 > article >正文

自动化测试中 iframe 相关问题与解决方案拓展

基础在Selenium 处理 iframe 和 iframe 的基础介绍-CSDN博客,不过这里也有部分重复喵

iframe 的基本切换

切换到 iframe

Selenium 提供 switch_to.frame() 方法:

# 根据 iframe 的 ID 切换
driver.switch_to.frame("iframe_id")

# 根据 iframe 的 name 切换
driver.switch_to.frame("iframe_name")

# 根据 WebElement 切换
iframe_element = driver.find_element(By.TAG_NAME, "iframe")
driver.switch_to.frame(iframe_element)
切回主页面

执行完 iframe 内部的操作后,需切回主页面:

driver.switch_to.default_content()

处理 iframe 内的动态元素

如果 iframe 内部的元素是 Ajax 动态加载 的,即使 iframe 已经切换成功,仍然可能找不到元素。

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

# 先切换到 iframe
driver.switch_to.frame("iframe_id")

# 等待 iframe 内的按钮加载完成
button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "submit_button"))
)
button.click()

# 切回主页面
driver.switch_to.default_content()

处理 iframe 内的元素

iframe 内找到元素时,要确保已经成功切换到 iframe

# 先切换到 iframe
driver.switch_to.frame("iframe_id")

# 访问 iframe 内部的元素
button = driver.find_element(By.ID, "submit_button")
button.click()

# 切回主页面
driver.switch_to.default_content()


http://www.kler.cn/a/611762.html

相关文章:

  • 【人工智能】机器学习中的评价指标
  • Prompt Engineering:如何写出更高效的提示词?
  • Tomcat日志中的404错误怎么处理
  • Spring AI + DeepSeek 构建大模型应用 Demo
  • 快速入手-基于Django-rest-framework的APIView类升级版GenericAPIView(四)
  • 闭包、装饰器学习笔记(第二次学习)
  • Java基础关键_030_线程(三)
  • 347 前k个高频元素
  • 多 线 程
  • WPF中的Adorner基础用法详解与实例
  • True strength lies in embracing vulnerability as a gateway to growth.
  • 23种设计模式-责任链(Chain of Responsibility)设计模式
  • AigcPanel v0.8.1 焕新上线:界面升级,新增 Spark - TTS、Fish Speech 模型支持
  • [Effective C++]条款24:若所有参数皆需类型转换,请为此采用non-menber函数
  • 【go微服务】跨语言RPC接口工具--protobuf语法与原理解读以及应用实战
  • [MRCTF2020]套娃
  • 实战 | 基于 SpringBoot + UniApp 打造国际版打车系统:架构设计与性能优化全解析
  • SQL优化 | OceanBase是否遵循最左匹配原则?(三)
  • MDC的原理是什么?
  • 计算机二级(C语言)考试高频考点总汇(二)—— 控制流、函数、数组和指针