Categories
Python Answers

How to select iframe using Python Selenium?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *