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