简体   繁体   English

如何单击 iframe 中的内容?

[英]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.

相关问题 Selenium:无法点击 iframe 中的按钮 - Selenium: Can't click on a button within an iframe 如何使用 BeautifulSoup 在此 iframe 中获取 src? - How can I grab the src within this iframe using BeautifulSoup? 单击 iframe 中的按钮 - pyppeteer - Click a button within an iframe - pyppeteer 如何使用 Selenium 和 Python 访问 iframe 中的输入字段 - How can I access the input field within an iframe using Selenium and Python 如何按索引 select 包含在 Playwright(python)中的 iFrame 中的按钮? - How can I select a button contained within an iFrame in Playwright (python) by index? 问:如何使用 Python 在 Web 内容中动态创建 iframe? - Q: How can I create dynamically an iframe inside a web content using Python? 如何在单击选项中使用 python-click 参数(检查文件中是否存在变量)? - How can I use an python-click argument from within a click option (check if variable exists in file)? 如何点击iframe中的元素? - How to click on an element in the iframe? 无法点击 iframe 中的下拉菜单 - Selenium Python - Unable to click on dropdown within iframe - Selenium Python Python Selenium:在iframe中获取动态内容 - Python Selenium: Getting dynamic content within iframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM