Frame
At every point of time, page exposes its current frame tree via the Page.mainFrame() and Frame.childFrames() methods.
Frame object's lifecycle is controlled by three events, dispatched on the page object:
- Page.onFrameAttached(handler) - fired when the frame gets attached to the page. A Frame can be attached to the page only once.
- Page.onFrameNavigated(handler) - fired when the frame commits navigation to a different URL.
- Page.onFrameDetached(handler) - fired when the frame gets detached from the page. A Frame can be detached from the page only once.
An example of dumping frame tree:
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType firefox = playwright.firefox();
Browser browser = firefox.launch();
Page page = browser.newPage();
page.navigate("https://www.google.com/chrome/browser/canary.html");
dumpFrameTree(page.mainFrame(), "");
browser.close();
}
}
static void dumpFrameTree(Frame frame, String indent) {
System.out.println(indent + frame.url());
for (Frame child : frame.childFrames()) {
dumpFrameTree(child, indent + " ");
}
}
}
Methods
addScriptTag
Added before v1.9Returns the added tag when the script's onload fires or when the script content was injected into frame.
Adds a <script> tag into the page with the desired url or content.
Usage
Frame.addScriptTag();
Frame.addScriptTag(options);
Arguments
optionsFrame.AddScriptTagOptions(optional)