通过Selenium和python切换到iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44834358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch to an iframe through Selenium and python
提问by Sarah H.
How would I switch to this iframe in selenium knowing only
我将如何在 selenium 中切换到这个 iframe 只知道
<iframe name="Dialogue Window">
回答by budi
You can use an XPath to locate the <iframe>
:
您可以使用 XPath 来定位<iframe>
:
iframe = driver.find_element_by_xpath("//iframe[@name='Dialogue Window']")
Then switch_to
the <iframe>
:
然后switch_to
在<iframe>
:
driver.switch_to.frame(iframe)
Here's how to switch back to the default content (out of the <iframe>
):
以下是切换回默认内容(在 之外<iframe>
)的方法:
driver.switch_to.default_content()
回答by DebanjanB
As per the HTML of the <iframe>
element, it has the nameattribute set as Dialogue Window. So to switch within the <iframe>
you need to use the switch_to()
method and you can use either of the following approaches:
根据<iframe>
元素的 HTML ,它的name属性设置为Dialogue Window。因此,要在<iframe>
您需要使用该switch_to()
方法的范围内切换,您可以使用以下任一方法:
Using the nameattribute of the
<iframe>
node as follows:# driver.switch_to.frame(‘frame_name') driver.switch_to.frame("Dialogue Window")
Using the
<iframe>
WebElementidentified through nameattribute as follows:driver.switch_to.frame(driver.find_element_by_name('Dialogue Window'))
Using the
<iframe>
WebElementidentified through css-selectorsas follows:driver.switch_to.frame(driver.find_element_by_css_selector("iframe[name='Dialogue Window']"))
Using the
<iframe>
WebElementidentified through xpathas follows:driver.switch_to.frame(driver.find_element_by_css_selector("//iframe[@name='Dialogue Window']"))
Ideally, you should induce WebDriverWaitinconjunction with expected_conditionsas
frame_to_be_available_and_switch_to_it()
for the desired frameas follows:Using
NAME
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"Dialogue Window")))
Using
CSS_SELECTOR
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='Dialogue Window']")))
Using
XPATH
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='Dialogue Window']")))
To switch back to the Parent Frameyou can use the following line of code:
driver.switch_to.parent_frame()
To switch back to the Top Level Browsing Context/ Top Windowyou can use the following line of code:
driver.switch_to.default_content()
使用节点的name属性
<iframe>
如下:# driver.switch_to.frame(‘frame_name') driver.switch_to.frame("Dialogue Window")
使用通过name属性标识的
<iframe>
WebElement如下:driver.switch_to.frame(driver.find_element_by_name('Dialogue Window'))
使用通过css-selectors标识的
<iframe>
WebElement如下:driver.switch_to.frame(driver.find_element_by_css_selector("iframe[name='Dialogue Window']"))
使用通过xpath标识的
<iframe>
WebElement如下:driver.switch_to.frame(driver.find_element_by_css_selector("//iframe[@name='Dialogue Window']"))
理想情况下,应当诱导WebDriverWaitinconjunction与expected_conditions作为
frame_to_be_available_and_switch_to_it()
用于期望帧如下:使用
NAME
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"Dialogue Window")))
使用
CSS_SELECTOR
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='Dialogue Window']")))
使用
XPATH
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='Dialogue Window']")))
要切换回父框架,您可以使用以下代码行:
driver.switch_to.parent_frame()
要切换回顶级浏览上下文/顶级窗口,您可以使用以下代码行:
driver.switch_to.default_content()