Frame表单切换

WebDriver只能在一个页面上对元素识别和定位,对于frame/iframe表单内嵌页面上的元素无法直接定位,需要通过switch_to.frame()方法将当前定位的主题切换为frame/iframe表单的内嵌页面中。

switch_to.frame()方法默认可以直接取表单的id或name属性,如果没有可用的id或name,需要先通过xpath定位到iframe(),再将定位元素传到switch_to.frame()方法。

完成表单操作后,可以通过switch_to.parent_content()方法跳出当前表单,也可以通过switch_to.default_content()跳回最外层的页面。

 

 

例子:

复制代码
"""
        将焦点转换到某个frame
        driver  浏览器驱动
        frameID frame的XPATH
        clickID 如果需要点击父页面的ID
"""
def go_to_frame_by_xpath(self, driver, frameID, clickID):
  driver.switch_to.default_content()
  if clickID != None:
    # 点击父页面的按钮,出现iframe
    WebDriverWait(driver, 30, 1).until(EC.presence_of_element_located((By.XPATH, clickID))).click()
   frameElement = WebDriverWait(driver, 30, 1).until(EC.presence_of_element_located((By.XPATH, frameID)))
   #将焦点转到iframe
  driver.switch_to.frame(frameElement)
复制代码

 

调用:

1
frame().go_to_frame_by_xpath(driver, '//*[@id="ifrm_a219d027-57e2-4b95-a598-a1b600f8ab4d"]', None)

  

posted on   瞎学瞎写  阅读(246)  评论(0编辑  收藏  举报

编辑推荐:
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
阅读排行:
· 对象命名为何需要避免'-er'和'-or'后缀
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· SQL Server如何跟踪自动统计信息更新?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示