我从 Selenium 开始并有一个问题,基本上我不明白为什么当我切换到 iframe 并选择我经常得到的元素之一时:
I'm starting with Selenium and have a question, basically I don't understand why when I switch to an iframe and select one of the elements I constantly get:
selenium.common.exceptions.NoSuchWindowException: Message: no such window
(Session info: chrome=84.0.4147.89)
这是相关代码,首先我单击包含 iframe 的 DOM 的某些元素,该元素实际上更新 iframe 内容,然后选择 iframe,切换到它,然后在 iframe 的 HTML 内容中选择我想要的元素
This is the relevan code, firstly I click on some element of the DOM containing the iframe that actually updates the iframe content and then select the iframe, switch to it and then select the element I want inside the HTML content of the iframe
# Select the item on main DOM that will udpate the iframe contents
bandeja = driver.find_element_by_xpath("//*[@id='sm20']")
bandeja.click()
# Tried sleeping, but also WedDriverWait to no avails...
time.sleep(3)
# Get the frame that actually has the content
frame = driver.find_element(by=By.NAME, value="ifrInterior")
driver.switch_to.frame(frame)
# Select the element inside the iframe and click
equipo = driver.find_element_by_xpath("//input[@name='gestionesPropias_Equipo']")
equipo.click()
现在如果我调试它,我没有收到上面的错误,我可以看到那里的元素已被选中...但点击后没有任何反应
Now if I debug this, I don't get the error above, and I can see there element has been selected...but upon click nothing happens
这是预期的元素,甚至检查了base64中的屏幕截图,它看起来还不错,它是一个单选输入按钮......只是想知道为什么点击不起作用??
This is the expected element, even checked the screenshot in base64 and it looks ok, it's a radio input button...just wondering why click doesn't work??
更新:根据@DebanjanB 的回复,我使用了他的建议,这可行,但按钮没有被点击
UPDATE: Based on response from @DebanjanB I used his sugestion and this works but the button does not get clicked
点击后应该会启用一个新的下拉菜单
Upon clicking there should a new drop down be enabled
这是单选按钮:
点击此下拉菜单时启用:
When clicked this drop-down is enabled:
该元素的相关代码
<span id="filtroAsignado" class="W30">
<select name="nuumaAsignado" class="W84">
<option value="">[Todo mi equipo]</option></select>
</span>
所以现在我想知道为什么它没有被点击?
So now I wonder why this does not get clicked?
由于所需元素位于 中,因此您必须:
As the desired element is within an <iframe>
so you have to:
诱导 WebDriverWait 使所需的框架可用并切换到它.
诱导 WebDriverWait使所需的元素可点击.
您可以使用以下 xpath 基于定位器策略:
You can use the following xpath based Locator Strategy:
使用 CSS_SELECTOR
:
# Select the item on main DOM that will udpate the iframe contents
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#sm20"))).click()
# Don't sleep, but only WedDriverWait...
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[value='ifrInterior']")))
# Select the element inside the iframe and click
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='gestionesPropias_Equipo']"))).click()
使用 XPATH
:
# Select the item on main DOM that will udpate the iframe contents
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='sm20']"))).click()
# Don't sleep, but only WedDriverWait...
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@value='ifrInterior']")))
# Select the element inside the iframe and click
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='gestionesPropias_Equipo']"))).click()
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些相关讨论:
You can find a couple of relevant discussions in:
NoSuchWindowException:没有这样的窗口:窗口已关闭",同时通过 Python3 使用 Selenium 和 WebDriver 切换选项卡
这篇关于selenium.common.exceptions.NoSuchWindowException:消息:使用 Selenium 和 Python 在 iframe 中单击元素时没有此类窗口错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!