Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Iframe Src content using Selenium Python

I have added to an html file: maintenance.html an iframe:

<iframe name="iframe_name" src="maintenance_state.txt" frameborder="0" height="40" allowtransparency="allowtransparency" width="800" align="middle" ></iframe>

And I want to get the content of the src file maintenance_state.txt using Python and Selenium. I'm locating the iframe element using:

maintain = driver.find_element_by_name("iframe_name")

However maintain.text is returning an empty value. How can I get the text written in maintenance_state.txt file.

Thanks for your help.

like image 230
user1542567 Avatar asked Aug 05 '15 14:08

user1542567


1 Answers

As some sites' scripts stop the iframe from working properly if it's loaded as the main document, it's also worth knowing how to read the iframe's source without needing to issue a separate driver.get for its URL:

driver.switch_to.frame(driver.find_element_by_name("iframe_name"))
print(driver.page_source)
driver.switch_to.default_content()

The last line is needed only if you want to be able to do something else with the page afterwards.

like image 149
Silas S. Brown Avatar answered Sep 28 '22 13:09

Silas S. Brown