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 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 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