[英]How can I click on content within an iframe?
I am trying to use Selenium to click buttons on a web page.我正在尝试使用 Selenium 单击 web 页面上的按钮。 I use this code:我使用这段代码:
driver.find_element(By.Xpath, '//*[@id="payments"]').click()
Normally this works, but it does not seem to be able to click the button shown in this HTML:通常这是可行的,但似乎无法单击此 HTML 中显示的按钮:
<iframe src="/ProviderPortal" id="main-iframe" frameborder="0" style="min-height: 600px; height: 600px;">
<a href="/ProviderPortal/Payment" id="payments">Payments</a>
</iframe>
When I run the code, nothing happens - it does not proceed to the next URL.当我运行代码时,什么都没有发生 - 它不会继续到下一个 URL。 The other buttons, that were successfully clicked, were not within an iframe.成功单击的其他按钮不在 iframe 内。 Is this the reason for the problem?这是问题的原因吗? How can I make it click successfully?我怎样才能让它成功点击?
In order to access web element(s) inside an iframe you need to switch to the iframe first.为了访问 iframe 中的 web 元素,您需要先切换到 iframe。 Like the following:如下所示:
driver.switch_to.frame(driver.find_element_by_id('main-iframe'))
driver.find_element(By.Xpath, '//*[@id="payments"]').click()
Once you finished working inside an iframe you will have to switch out from the iframe to the default content with完成 iframe 内部的工作后,您必须使用以下命令从 iframe 切换到默认内容
driver.switch_to.default_content()
First you need to switch to that iframe by: drive.switch_to.frame(1)首先,您需要通过以下方式切换到 iframe:drive.switch_to.frame(1)
Then swicth back to main content:然后切换回主要内容:
driver.switch_to.default_content() driver.switch_to.default_content()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.