pub struct Frame { /* private fields */ }Expand description
Frame represents a frame within a page.
Every page has a main frame, and pages can have additional child frames (iframes). Frame is where navigation, selector queries, and DOM operations actually happen.
In Playwright’s architecture, Page delegates navigation and interaction methods to Frame.
Implementations§
Source§impl Frame
impl Frame
Sourcepub fn new(
parent: Arc<dyn ChannelOwner>,
type_name: String,
guid: Arc<str>,
initializer: Value,
) -> Result<Self>
pub fn new( parent: Arc<dyn ChannelOwner>, type_name: String, guid: Arc<str>, initializer: Value, ) -> Result<Self>
Creates a new Frame from protocol initialization.
This is called by the object factory when the server sends a __create__ message
for a Frame object.
Sourcepub fn page(&self) -> Option<Page>
pub fn page(&self) -> Option<Page>
Returns the owning Page for this frame, if it has been set.
Returns None if set_page() has not been called yet (i.e., before the frame
has been adopted by a Page). In normal usage the main frame always has a Page.
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the name attribute value of the frame element used to create this frame.
For the main (top-level) frame this is always an empty string.
Sourcepub fn parent_frame(&self) -> Option<Frame>
pub fn parent_frame(&self) -> Option<Frame>
Returns the parent Frame, or None if this is the top-level (main) frame.
See: https://playwright.dev/docs/api/class-frame#frame-parent-frame
Sourcepub fn is_detached(&self) -> bool
pub fn is_detached(&self) -> bool
Returns true if the frame has been detached from its page.
A frame becomes detached when the corresponding <iframe> element is removed
from the DOM or when the owning page is closed.
See: https://playwright.dev/docs/api/class-frame#frame-is-detached
Sourcepub fn child_frames(&self) -> Vec<Frame>
pub fn child_frames(&self) -> Vec<Frame>
Returns all child frames embedded in this frame.
Child frames are created by <iframe> elements within this frame.
For the main frame this may include multiple iframes.
§Implementation Note
This iterates all objects in the connection registry to find Frame objects
whose parentFrame initializer field matches this frame’s GUID. This matches
the relationship Playwright establishes when creating child frames.
See: https://playwright.dev/docs/api/class-frame#frame-child-frames
Sourcepub async fn evaluate_handle(
&self,
expression: &str,
) -> Result<Arc<ElementHandle>>
pub async fn evaluate_handle( &self, expression: &str, ) -> Result<Arc<ElementHandle>>
Evaluates a JavaScript expression and returns a handle to the result.
Unlike evaluate which serializes the return value to JSON,
evaluate_handle returns a handle to the in-browser object. This is useful when
the return value is a non-serializable DOM element or complex JS object.
§Arguments
expression- JavaScript expression to evaluate in the frame context
§Returns
An Arc<ElementHandle> pointing to the in-browser object.
§Example
let playwright = Playwright::launch().await?;
let browser = playwright.chromium().launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", None).await?;
let frame = page.main_frame().await?;
let handle = frame.evaluate_handle("document.body").await?;
let screenshot = handle.screenshot(None).await?;§Errors
Returns error if:
- The JavaScript expression throws an error
- The result handle GUID cannot be found in the registry
- Communication with the browser fails
See: https://playwright.dev/docs/api/class-frame#frame-evaluate-handle
Sourcepub fn locator(&self, selector: &str) -> Locator
pub fn locator(&self, selector: &str) -> Locator
Creates a Locator scoped to this frame.
The locator is lazy — it does not query the DOM until an action is performed on it.
§Arguments
selector- A CSS selector or other Playwright selector strategy
§Panics
Panics if the owning Page has not been set (i.e., set_page() was never called).
In normal usage the main frame always has its page wired up by Page::main_frame().
See: https://playwright.dev/docs/api/class-frame#frame-locator
Sourcepub fn get_by_text(&self, text: &str, exact: bool) -> Locator
pub fn get_by_text(&self, text: &str, exact: bool) -> Locator
Returns a locator that matches elements containing the given text.
See: https://playwright.dev/docs/api/class-frame#frame-get-by-text
Sourcepub fn get_by_label(&self, text: &str, exact: bool) -> Locator
pub fn get_by_label(&self, text: &str, exact: bool) -> Locator
Returns a locator that matches elements by their associated label text.
See: https://playwright.dev/docs/api/class-frame#frame-get-by-label
Sourcepub fn get_by_placeholder(&self, text: &str, exact: bool) -> Locator
pub fn get_by_placeholder(&self, text: &str, exact: bool) -> Locator
Returns a locator that matches elements by their placeholder text.
See: https://playwright.dev/docs/api/class-frame#frame-get-by-placeholder
Sourcepub fn get_by_alt_text(&self, text: &str, exact: bool) -> Locator
pub fn get_by_alt_text(&self, text: &str, exact: bool) -> Locator
Returns a locator that matches elements by their alt text.
See: https://playwright.dev/docs/api/class-frame#frame-get-by-alt-text
Sourcepub fn get_by_title(&self, text: &str, exact: bool) -> Locator
pub fn get_by_title(&self, text: &str, exact: bool) -> Locator
Returns a locator that matches elements by their title attribute.
See: https://playwright.dev/docs/api/class-frame#frame-get-by-title
Sourcepub fn get_by_test_id(&self, test_id: &str) -> Locator
pub fn get_by_test_id(&self, test_id: &str) -> Locator
Returns a locator that matches elements by their data-testid attribute.
See: https://playwright.dev/docs/api/class-frame#frame-get-by-test-id
Sourcepub fn get_by_role(
&self,
role: AriaRole,
options: Option<GetByRoleOptions>,
) -> Locator
pub fn get_by_role( &self, role: AriaRole, options: Option<GetByRoleOptions>, ) -> Locator
Returns a locator that matches elements by their ARIA role.
See: https://playwright.dev/docs/api/class-frame#frame-get-by-role
Sourcepub fn url(&self) -> String
pub fn url(&self) -> String
Returns the current URL of the frame.
This returns the last committed URL. Initially, frames are at “about:blank”.
Sourcepub async fn goto(
&self,
url: &str,
options: Option<GotoOptions>,
) -> Result<Option<Response>>
pub async fn goto( &self, url: &str, options: Option<GotoOptions>, ) -> Result<Option<Response>>
Navigates the frame to the specified URL.
This is the actual protocol method for navigation. Page.goto() delegates to this.
Returns None when navigating to URLs that don’t produce responses (e.g., data URLs,
about:blank). This matches Playwright’s behavior across all language bindings.
§Arguments
url- The URL to navigate tooptions- Optional navigation options (timeout, wait_until)
Sourcepub async fn title(&self) -> Result<String>
pub async fn title(&self) -> Result<String>
Returns the frame’s title.
See: https://playwright.dev/docs/api/class-frame#frame-title
Sourcepub async fn content(&self) -> Result<String>
pub async fn content(&self) -> Result<String>
Returns the full HTML content of the frame, including the DOCTYPE.
See: https://playwright.dev/docs/api/class-frame#frame-content
Sourcepub async fn set_content(
&self,
html: &str,
options: Option<GotoOptions>,
) -> Result<()>
pub async fn set_content( &self, html: &str, options: Option<GotoOptions>, ) -> Result<()>
Sets the content of the frame.
See: https://playwright.dev/docs/api/class-frame#frame-set-content
Sourcepub async fn wait_for_load_state(&self, state: Option<WaitUntil>) -> Result<()>
pub async fn wait_for_load_state(&self, state: Option<WaitUntil>) -> Result<()>
Waits for the required load state to be reached.
Playwright’s protocol doesn’t expose waitForLoadState as a server-side command —
it’s implemented client-side using lifecycle events. We implement it by polling
document.readyState via JavaScript evaluation.
See: https://playwright.dev/docs/api/class-frame#frame-wait-for-load-state
Sourcepub async fn wait_for_url(
&self,
url: &str,
options: Option<GotoOptions>,
) -> Result<()>
pub async fn wait_for_url( &self, url: &str, options: Option<GotoOptions>, ) -> Result<()>
Waits for the frame to navigate to a URL matching the given string or glob pattern.
Playwright’s protocol doesn’t expose waitForURL as a server-side command —
it’s implemented client-side. We implement it by polling window.location.href.
See: https://playwright.dev/docs/api/class-frame#frame-wait-for-url
Sourcepub async fn query_selector(
&self,
selector: &str,
) -> Result<Option<Arc<ElementHandle>>>
pub async fn query_selector( &self, selector: &str, ) -> Result<Option<Arc<ElementHandle>>>
Returns the first element matching the selector, or None if not found.
See: https://playwright.dev/docs/api/class-frame#frame-query-selector
Sourcepub async fn query_selector_all(
&self,
selector: &str,
) -> Result<Vec<Arc<ElementHandle>>>
pub async fn query_selector_all( &self, selector: &str, ) -> Result<Vec<Arc<ElementHandle>>>
Returns all elements matching the selector.
See: https://playwright.dev/docs/api/class-frame#frame-query-selector-all
Sourcepub async fn evaluate<T: Serialize>(
&self,
expression: &str,
arg: Option<&T>,
) -> Result<Value>
pub async fn evaluate<T: Serialize>( &self, expression: &str, arg: Option<&T>, ) -> Result<Value>
Evaluates a JavaScript expression in the frame context with optional arguments.
Executes the provided JavaScript expression within the frame’s context and returns the result. The return value must be JSON-serializable.
§Arguments
expression- JavaScript code to evaluatearg- Optional argument to pass to the expression (must implement Serialize)
§Returns
The result as a serde_json::Value
§Example
use serde_json::json;
use playwright_rs::protocol::Playwright;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let playwright = Playwright::launch().await?;
let browser = playwright.chromium().launch().await?;
let page = browser.new_page().await?;
let frame = page.main_frame().await?;
// Evaluate without arguments
let result = frame.evaluate::<()>("1 + 1", None).await?;
// Evaluate with argument
let arg = json!({"x": 5, "y": 3});
let result = frame.evaluate::<serde_json::Value>("(arg) => arg.x + arg.y", Some(&arg)).await?;
assert_eq!(result, json!(8));
Ok(())
}See: https://playwright.dev/docs/api/class-frame#frame-evaluate
Sourcepub async fn add_style_tag(
&self,
options: AddStyleTagOptions,
) -> Result<Arc<ElementHandle>>
pub async fn add_style_tag( &self, options: AddStyleTagOptions, ) -> Result<Arc<ElementHandle>>
Adds a <style> tag into the page with the desired content.
§Arguments
options- Style tag options (content, url, or path)
At least one of content, url, or path must be specified.
§Example
use playwright_rs::protocol::AddStyleTagOptions;
// With inline CSS
frame.add_style_tag(
AddStyleTagOptions::builder()
.content("body { background-color: red; }")
.build()
).await?;
// With URL
frame.add_style_tag(
AddStyleTagOptions::builder()
.url("https://example.com/style.css")
.build()
).await?;See: https://playwright.dev/docs/api/class-frame#frame-add-style-tag
Sourcepub async fn add_script_tag(
&self,
options: AddScriptTagOptions,
) -> Result<Arc<ElementHandle>>
pub async fn add_script_tag( &self, options: AddScriptTagOptions, ) -> Result<Arc<ElementHandle>>
Adds a <script> tag into the frame with the desired content.
§Arguments
options- Script tag options (content, url, or path)
At least one of content, url, or path must be specified.
See: https://playwright.dev/docs/api/class-frame#frame-add-script-tag