How would I switch to this iframe in selenium knowing only
As per the HTML of the element, it has the name attribute set as Dialogue Window. So to switch within the
you need to use the switch_to() method and you can use either of the following approaches:
Using the name attribute of the node as follows:
# driver.switch_to.frame(‘frame_name’)
driver.switch_to.frame("Dialogue Window")
Using the WebElement identified through name attribute as follows:
driver.switch_to.frame(driver.find_element_by_name('Dialogue Window'))
Using the WebElement identified through css-selectors as follows:
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[name='Dialogue Window']"))
Using the WebElement identified through xpath as follows:
driver.switch_to.frame(driver.find_element_by_css_selector("//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']")))
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()
Ways to deal with #document under iframe