selenium.common.exceptions.NoSuchWindowException: Message: no such window error clicking an element within an iframe using Selenium and Python

倾然丶 夕夏残阳落幕 提交于 2021-02-02 09:53:45

问题


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)

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

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??

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

This is the radio button:

When clicked this drop-down is enabled:

The relevant code about this element

<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?


回答1:


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.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use the following xpath based Locator Strategy:

  • Using 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()
    
  • Using 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
    

Reference

You can find a couple of relevant discussions in:

  • Ways to deal with #document under iframe
  • Switch to an iframe through Selenium and python

tl; dr

“NoSuchWindowException: no such window: window was already closed” while switching tabs using Selenium and WebDriver through Python3



来源:https://stackoverflow.com/questions/63094654/selenium-common-exceptions-nosuchwindowexception-message-no-such-window-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!