A web page may have different frames, and even some frames inside frames. If you want to do things related to a specific frame, like evaluating code, the webpage object have some methods to select the frame you want to work with.
These methods are:
- webpage.switchToFocusedFrame(): to select the frame that has the focus. You can retrieve its name with webpage.focusedFrameName.
- webpage.switchToFrame(framename) (or webpage.switchToChildFrame()): to select a frame (in the current frame) that has the given name. You can retrieve the list of frame names with framesName, and the count with framesCount.
- webpage.switchToParentFrame(): to select the parent frame of the current frame
- webpage.switchToMainFrame(): to return to the main frame (i.e. the root window)
root window:
<frameset>
<frame name="f1" src="frame1.html" />
<frame name="f2" src="frame2.html" />
</frameset>
frame1.html:
<html>... <iframe name="f3" src="frame3.html">... </html>
frame2.html and frame3.html:
<html>... simple web page ....</html>
To select frame2.html, you’ll do:
webpage.switchToFrame('f2');
If you want to select the frame3.html after that, you’ll do:
// first return to the parent frame
webpage.switchToParentFrame(); // or switchToMainFrame() in this example
// then select frame1.html because frame3.html is a child of it
webpage.switchToFrame('f1');
// then select frame3.html
webpage.switchToFrame('f3');
After selecting a frame, you have some properties to retrieve the frame properties: webpage.frameContent, webpage.frameTitle, webpage.frameName, webpage.frameUrl, webpage.framePlainText.
And you can evaluate javascript in the current frame with evaluateJavaScript(), evaluate(), evaluateAsync(), includeJs() or injectJs()