Menu Close

How to select iframe using Python Selenium?

Sometimes, we want to select iframe using Python Selenium.

In this article, we’ll look at how to select iframe using Python Selenium.

How to select iframe using Python Selenium?

To select iframe using Python Selenium, we can call switch_to.frame.

For instance, we write

self.driver = webdriver.Firefox()

time.sleep(3)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

elem = driver.find_element_by_xpath("/html/body/p")
elem.send_keys("Lorem Ipsum")
driver.switch_to.default_content()

to write

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

to switch to the first iframe found by find_element_by_tag_name.

Then we do some manipulation in the iframe with

elem = driver.find_element_by_xpath("/html/body/p")
elem.send_keys("Lorem Ipsum")

then we switch back to the original page with

driver.switch_to.default_content()

Conclusion

To select iframe using Python Selenium, we can call switch_to.frame.

Posted in Python, Python Answers