selenium.common.exceptions.NoSuchWindowException:消息:使用Selenium和Python单击iframe中的元素时,没有此类窗口错误 [英] selenium.common.exceptions.NoSuchWindowException: Message: no such window error clicking an element within an iframe using Selenium and Python

查看:172
本文介绍了selenium.common.exceptions.NoSuchWindowException:消息:使用Selenium和Python单击iframe中的元素时,没有此类窗口错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从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)

这是relevan代码,首先,我单击包含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?

推荐答案

由于所需元素位于< iframe> 中,因此您必须:

As the desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

引发 WebDriverWait 使所需的元素可点击.

您可以使用以下基于定位器策略:

使用 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中的元素时,没有此类窗口错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆