python - I'm trying to switch frames in Selenium using Xpath, but haven't had any success so far
问题描述
I'm trying to switch frames in Selenium using an Xpath instead of using the name of the frame. The frame doesn't have a name so I figured I could just use the Xpath, but I'm not sure Selenium supports using an Xpath instead of a name.
This is the normal way to switch frames:
driver.switch_to.frame("WhateverFrame")
This is what I have tried:
driver.switch_to.frame(By.XPath("//*[@id='ui-id-1']/iframe"))
driver.find_frame_by_xpath("//*[@id='ui-id-43']/iframe")
Any suggestions as to how I should alter my code to get this to work?
解决方案
您可以使用不同的选项切换到框架。
driver.switch_to.frame('frame_name')
driver.switch_to.frame(frame_index)
driver.switch_to.frame(element)
因此,在您的情况下,您可以发送元素,如下所示。
driver.switch_to.frame(driver.find_element_by_xpath("//*[@id='ui-id-43']/iframe"))
如果您想转到第一个 iframe,您也可以执行以下操作。
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
推荐阅读
- algorithm - 什么是删除不属于至少一个循环的图的所有边的好算法?
- java - Hibernate HQL 是否支持正则表达式模式匹配?
- excel - 当 Cell(W10) 的值 >= Cell(R10) 的值时需要哔声,这是针对 W 和 R 的整个列
- javascript - 如何将变量从 c# 传递到 javascript 以在 plotly.js 中绘图
- python - SQL alchemy 对 LIMIT 查询应用默认过滤器
- tensorflow - 如何在不考虑方差的情况下去除 Keras 层的均值,例如 Batchnormalization?
- reactjs - 是否可以有条件地仅在 React 的输入标签中呈现占位符属性?
- amazon-web-services - 在 Athena 中查询表时获取 HIVE_CURSOR_ERROR
- python - tkinter 滚动条在调整窗口大小之前处于非活动状态
- rust - 将 TaskManager 脚本从 gdscript/Godot 迁移到 bevy 和 rust