Problem with iFrames in Selenium

I am trying to clear a webpage using Selenium (in Python), almost entirely Javascript.
For example, this is the body of the page:

<body class="bodyLoading"> <!-- this is required for GWT history support --> <iframe id="__gwt_historyFrame" role="presentation" width="0" height="0" tabindex="-1" title="empty" style="position:absolute;width:0;height:0;border:0" src="javascript:''"> </iframe> <!-- For printing window contents --> <iframe id="__printingFrame" role="presentation" width="0" height="0" tabindex="-1" title="empty" style="width:0;height:0;border:0;" /> <!-- TODO : RECOMMENDED if your web app will not function without JavaScript enabled --> <noscript> <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif"> Your web browser must have JavaScript enabled in order for Regulations.gov to display correctly. </div> </noscript> </body> 

For some reason, selenium (using the Firefox engine) does not evaluate javascript on this page. If I use the get_html_source function, it just returns the html above and not the imported JavaScript JavaScript that I can see in my browser (and in the Selenium browser). And unfortunately, I cannot understand that the src attibute from iFrame just says javascript: which I cannot understand.

Any thoughts on how to make sure Selenium is handling this iFrame?

+4
source share
1 answer

IFrames are separate documents, so you won’t get their contents included in the HTML code for the main page; you must read them separately.

You can do this using the Selenium select_frame function.

You can access the frame through its name, CSS selector, xpath link, etc., as for other elements.

When you select a frame, you change the Selenium context, so you can access the contents of the frame as if it were the current page.

If you have frames inside frames, you can continue this process through the frame tree.

Obviously, you need a way to return the backup path. Selenium provides this by letting you use the same select_frame function, with either relative=up to move the context to the parent of the current frame, or relative=top to go to the main page in the browser.

Thus, using this function, you can move around the frames on the page.

You cannot access all at once; only one frame can be in the context at once, so you can never make one call to get_html_source and immediately get the entire contents of the frames, but you can move around the frames on the page in your selenium script and get the HTML source for each frame separately.

Hope this helps.

+4
source

All Articles