30

How would I switch to this iframe in selenium knowing only

<iframe name="Dialogue Window">
Asclepius
  • 49,954
  • 14
  • 144
  • 128
Sarah H.
  • 431
  • 1
  • 5
  • 12
  • 1
    See: [How do I do X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do-x) The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – JeffC Jan 29 '18 at 19:20

3 Answers3

82

You can use an XPath to locate the <iframe>:

iframe = driver.find_element_by_xpath("//iframe[@name='Dialogue Window']")

Then switch_to the <iframe>:

driver.switch_to.frame(iframe)

Here's how to switch back to the default content (out of the <iframe>):

driver.switch_to.default_content()
Asclepius
  • 49,954
  • 14
  • 144
  • 128
budi
  • 5,903
  • 8
  • 51
  • 77
  • 1
    Good general solution, especially if the iframe has to be targeted by something other than a `name` - which is really the only way that [the selenium docs](http://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames) specify transitioning – kevlarr Jan 18 '18 at 20:53
28

As per the HTML of the <iframe> element, it has the name attribute set as Dialogue Window. So to switch within the <iframe> you need to use the switch_to() method and you can use either of the following approaches:

  • Using the name attribute of the <iframe> node as follows:

    # driver.switch_to.frame(‘frame_name’)
    driver.switch_to.frame("Dialogue Window")
    
  • Using the <iframe> WebElement identified through name attribute as follows:

    driver.switch_to.frame(driver.find_element_by_name('Dialogue Window'))
    
  • Using the <iframe> WebElement identified through as follows:

    driver.switch_to.frame(driver.find_element_by_css_selector("iframe[name='Dialogue Window']"))
    
  • Using the <iframe> WebElement identified through as follows:

    driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@name='Dialogue Window']"))
    

Ideally, you should induce WebDriverWait inconjunction with expected_conditions as frame_to_be_available_and_switch_to_it() for the desired frame as follows:

  • Using NAME:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"Dialogue Window"))) 
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='Dialogue Window']")))
    
  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='Dialogue Window']")))
    

Switching back from frame

  • To switch back to the Parent Frame you can use the following line of code:

    driver.switch_to.parent_frame()
    
  • To switch back to the Top Level Browsing Context / Top Window you can use the following line of code:

    driver.switch_to.default_content()
    

tl; dr

Ways to deal with #document under iframe

Smart Manoj
  • 4,375
  • 4
  • 27
  • 51
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
0

I have resolved this issue in Python-Selenium. Please use the below code:

srtHandle = driver.window_handles
driver.switch_to_window(srtHandle[0])

Then switch to the frame in which the element is located.

Josef
  • 2,124
  • 1
  • 20
  • 21