pub struct Page { /* private fields */ }
Implementations§
Source§impl Page
impl Page
Sourcepub async fn enable_stealth_mode(&self) -> Result<()>
pub async fn enable_stealth_mode(&self) -> Result<()>
Changes your user_agent, removes the navigator.webdriver
property
changes permissions, pluggins rendering contexts and the window.chrome
property to make it harder to detect the scraper as a bot
Sourcepub async fn enable_stealth_mode_with_agent(&self, ua: &str) -> Result<()>
pub async fn enable_stealth_mode_with_agent(&self, ua: &str) -> Result<()>
Changes your user_agent with a custom agent, removes the navigator.webdriver
property
changes permissions, pluggins rendering contexts and the window.chrome
property to make it harder to detect the scraper as a bot
Sourcepub async fn enable_stealth_mode_with_agent_and_dimiss_dialogs(
&self,
ua: &str,
) -> Result<()>
pub async fn enable_stealth_mode_with_agent_and_dimiss_dialogs( &self, ua: &str, ) -> Result<()>
Changes your user_agent with a custom agent, removes the navigator.webdriver
property
changes permissions, pluggins rendering contexts and the window.chrome
property to make it harder to detect the scraper as a bot. Also add dialog polyfill to prevent blocking the page.
Sourcepub async fn hide_chrome(&self) -> Result<(), CdpError>
pub async fn hide_chrome(&self) -> Result<(), CdpError>
Sets window.chrome
on frame creation and console.log methods.
Sourcepub async fn hide_webgl_vendor(&self) -> Result<(), CdpError>
pub async fn hide_webgl_vendor(&self) -> Result<(), CdpError>
Obfuscates WebGL vendor on frame creation
Sourcepub async fn hide_plugins(&self) -> Result<(), CdpError>
pub async fn hide_plugins(&self) -> Result<(), CdpError>
Obfuscates browser plugins and hides the navigator object on frame creation
Sourcepub async fn hide_permissions(&self) -> Result<(), CdpError>
pub async fn hide_permissions(&self) -> Result<(), CdpError>
Obfuscates browser permissions on frame creation
Sourcepub async fn hide_webdriver(&self) -> Result<(), CdpError>
pub async fn hide_webdriver(&self) -> Result<(), CdpError>
Removes the navigator.webdriver
property on frame creation
Sourcepub async fn execute<T: Command>(
&self,
cmd: T,
) -> Result<CommandResponse<T::Response>>
pub async fn execute<T: Command>( &self, cmd: T, ) -> Result<CommandResponse<T::Response>>
Execute a command and return the Command::Response
Sourcepub fn command_future<T: Command>(&self, cmd: T) -> Result<CommandFuture<T>>
pub fn command_future<T: Command>(&self, cmd: T) -> Result<CommandFuture<T>>
Execute a command and return the Command::Response
Sourcepub fn http_future<T: Command>(&self, cmd: T) -> Result<HttpFuture<T>>
pub fn http_future<T: Command>(&self, cmd: T) -> Result<HttpFuture<T>>
Execute a command and return the Command::Response
Sourcepub async fn event_listener<T: IntoEventKind>(&self) -> Result<EventStream<T>>
pub async fn event_listener<T: IntoEventKind>(&self) -> Result<EventStream<T>>
Adds an event listener to the Target
and returns the receiver part as
EventStream
An EventStream
receives every Event
the Target
receives.
All event listener get notified with the same event, so registering
multiple listeners for the same event is possible.
Custom events rely on being deserializable from the received json params
in the EventMessage
. Custom Events are caught by the CdpEvent::Other
variant. If there are mulitple custom event listener is registered
for the same event, identified by the MethodType::method_id
function,
the Target
tries to deserialize the json using the type of the event
listener. Upon success the Target
then notifies all listeners with the
deserialized event. This means, while it is possible to register
different types for the same custom event, only the type of first
registered event listener will be used. The subsequent listeners, that
registered for the same event but with another type won’t be able to
receive anything and therefor will come up empty until all their
preceding event listeners are dropped and they become the first (or
longest) registered event listener for an event.
§Example Listen for canceled animations
let mut events = page.event_listener::<EventAnimationCanceled>().await?;
while let Some(event) = events.next().await {
//..
}
§Example Liste for a custom event
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
struct MyCustomEvent {
name: String,
}
impl MethodType for MyCustomEvent {
fn method_id() -> MethodId {
"Custom.Event".into()
}
}
impl CustomEvent for MyCustomEvent {}
let mut events = page.event_listener::<MyCustomEvent>().await?;
while let Some(event) = events.next().await {
//..
}
pub async fn expose_function( &self, name: impl Into<String>, function: impl AsRef<str>, ) -> Result<()>
This resolves once the navigation finished and the page is loaded.
This is necessary after an interaction with the page that may trigger a
navigation (click
, press_key
) in order to wait until the new browser
page is loaded
Same as wait_for_navigation_response
but returns Self
instead
Sourcepub async fn goto(&self, params: impl Into<NavigateParams>) -> Result<&Self>
pub async fn goto(&self, params: impl Into<NavigateParams>) -> Result<&Self>
Navigate directly to the given URL.
This resolves directly after the requested URL is fully loaded.
Sourcepub fn session_id(&self) -> &SessionId
pub fn session_id(&self) -> &SessionId
The identifier of the Session
target of this page is attached to
Sourcepub fn opener_id(&self) -> &Option<TargetId>
pub fn opener_id(&self) -> &Option<TargetId>
The identifier of the Session
target of this page is attached to
Sourcepub async fn frame_name(&self, frame_id: FrameId) -> Result<Option<String>>
pub async fn frame_name(&self, frame_id: FrameId) -> Result<Option<String>>
Returns the name of the frame
pub async fn authenticate(&self, credentials: Credentials) -> Result<()>
Sourcepub async fn frame_url(&self, frame_id: FrameId) -> Result<Option<String>>
pub async fn frame_url(&self, frame_id: FrameId) -> Result<Option<String>>
Returns the current url of the frame
Sourcepub async fn frame_parent(&self, frame_id: FrameId) -> Result<Option<FrameId>>
pub async fn frame_parent(&self, frame_id: FrameId) -> Result<Option<FrameId>>
Returns the parent id of the frame
Sourcepub async fn set_user_agent(
&self,
params: impl Into<SetUserAgentOverrideParams>,
) -> Result<&Self>
pub async fn set_user_agent( &self, params: impl Into<SetUserAgentOverrideParams>, ) -> Result<&Self>
Allows overriding user agent with the given string.
Sourcepub async fn user_agent(&self) -> Result<String>
pub async fn user_agent(&self) -> Result<String>
Returns the user agent of the browser
Sourcepub async fn get_document(&self) -> Result<Node>
pub async fn get_document(&self) -> Result<Node>
Returns the root DOM node (and optionally the subtree) of the page.
§Note: This does not return the actual HTML document of the page. To
retrieve the HTML content of the page see Page::content
.
Sourcepub async fn find_element(&self, selector: impl Into<String>) -> Result<Element>
pub async fn find_element(&self, selector: impl Into<String>) -> Result<Element>
Returns the first element in the document which matches the given CSS selector.
Execute a query selector on the document’s node.
Sourcepub async fn find_elements(
&self,
selector: impl Into<String>,
) -> Result<Vec<Element>>
pub async fn find_elements( &self, selector: impl Into<String>, ) -> Result<Vec<Element>>
Return all Element
s in the document that match the given selector
Sourcepub async fn find_xpath(&self, selector: impl Into<String>) -> Result<Element>
pub async fn find_xpath(&self, selector: impl Into<String>) -> Result<Element>
Returns the first element in the document which matches the given xpath selector.
Execute a xpath selector on the document’s node.
Sourcepub async fn find_xpaths(
&self,
selector: impl Into<String>,
) -> Result<Vec<Element>>
pub async fn find_xpaths( &self, selector: impl Into<String>, ) -> Result<Vec<Element>>
Return all Element
s in the document that match the given xpath selector
Sourcepub async fn describe_node(&self, node_id: NodeId) -> Result<Node>
pub async fn describe_node(&self, node_id: NodeId) -> Result<Node>
Describes node given its id
Sourcepub async fn close(self) -> Result<()>
pub async fn close(self) -> Result<()>
Tries to close page, running its beforeunload hooks, if any.
Calls Page.close with CloseParams
Sourcepub async fn click(&self, point: Point) -> Result<&Self>
pub async fn click(&self, point: Point) -> Result<&Self>
Performs a single mouse click event at the point’s location.
This scrolls the point into view first, then executes a
DispatchMouseEventParams
command of type MouseLeft
with
MousePressed
as single click and then releases the mouse with an
additional DispatchMouseEventParams
of type MouseLeft
with
MouseReleased
Bear in mind that if click()
triggers a navigation the new page is not
immediately loaded when click()
resolves. To wait until navigation is
finished an additional wait_for_navigation()
is required:
§Example
Trigger a navigation and wait until the triggered navigation is finished
let html = page.click(point).await?.wait_for_navigation().await?.content();
§Example
Perform custom click
// double click
let cmd = DispatchMouseEventParams::builder()
.x(point.x)
.y(point.y)
.button(MouseButton::Left)
.click_count(2);
page.move_mouse(point).await?.execute(
cmd.clone()
.r#type(DispatchMouseEventType::MousePressed)
.build()
.unwrap(),
)
.await?;
page.execute(
cmd.r#type(DispatchMouseEventType::MouseReleased)
.build()
.unwrap(),
)
.await?;
Sourcepub async fn move_mouse(&self, point: Point) -> Result<&Self>
pub async fn move_mouse(&self, point: Point) -> Result<&Self>
Dispatches a mousemove
event and moves the mouse to the position of
the point
where Point.x
is the horizontal position of the mouse and
Point.y
the vertical position of the mouse.
Sourcepub async fn screenshot(
&self,
params: impl Into<ScreenshotParams>,
) -> Result<Vec<u8>>
pub async fn screenshot( &self, params: impl Into<ScreenshotParams>, ) -> Result<Vec<u8>>
Take a screenshot of the current page
Sourcepub async fn save_screenshot(
&self,
params: impl Into<ScreenshotParams>,
output: impl AsRef<Path>,
) -> Result<Vec<u8>>
pub async fn save_screenshot( &self, params: impl Into<ScreenshotParams>, output: impl AsRef<Path>, ) -> Result<Vec<u8>>
Save a screenshot of the page
§Example save a png file of a website
page.goto("http://example.com")
.await?
.save_screenshot(
ScreenshotParams::builder()
.format(CaptureScreenshotFormat::Png)
.full_page(true)
.omit_background(true)
.build(),
"example.png",
)
.await?;
Sourcepub async fn pdf(&self, params: PrintToPdfParams) -> Result<Vec<u8>>
pub async fn pdf(&self, params: PrintToPdfParams) -> Result<Vec<u8>>
Print the current page as pdf.
See PrintToPdfParams
§Note Generating a pdf is currently only supported in Chrome headless.
Sourcepub async fn save_pdf(
&self,
opts: PrintToPdfParams,
output: impl AsRef<Path>,
) -> Result<Vec<u8>>
pub async fn save_pdf( &self, opts: PrintToPdfParams, output: impl AsRef<Path>, ) -> Result<Vec<u8>>
Save the current page as pdf as file to the output
path and return the
pdf contents.
§Note Generating a pdf is currently only supported in Chrome headless.
Sourcepub async fn bring_to_front(&self) -> Result<&Self>
pub async fn bring_to_front(&self) -> Result<&Self>
Brings page to front (activates tab)
Sourcepub async fn emulate_media_features(
&self,
features: Vec<MediaFeature>,
) -> Result<&Self>
pub async fn emulate_media_features( &self, features: Vec<MediaFeature>, ) -> Result<&Self>
Emulates the given media type or media feature for CSS media queries
Sourcepub async fn emulate_media_type(
&self,
media_type: impl Into<MediaTypeParams>,
) -> Result<&Self>
pub async fn emulate_media_type( &self, media_type: impl Into<MediaTypeParams>, ) -> Result<&Self>
Changes the CSS media type of the page
Sourcepub async fn emulate_timezone(
&self,
timezoune_id: impl Into<SetTimezoneOverrideParams>,
) -> Result<&Self>
pub async fn emulate_timezone( &self, timezoune_id: impl Into<SetTimezoneOverrideParams>, ) -> Result<&Self>
Overrides default host system timezone
Sourcepub async fn emulate_locale(
&self,
locale: impl Into<SetLocaleOverrideParams>,
) -> Result<&Self>
pub async fn emulate_locale( &self, locale: impl Into<SetLocaleOverrideParams>, ) -> Result<&Self>
Overrides default host system locale with the specified one
Sourcepub async fn emulate_geolocation(
&self,
geolocation: impl Into<SetGeolocationOverrideParams>,
) -> Result<&Self>
pub async fn emulate_geolocation( &self, geolocation: impl Into<SetGeolocationOverrideParams>, ) -> Result<&Self>
Overrides the Geolocation Position or Error. Omitting any of the parameters emulates position unavailable.
Sourcepub async fn reload(&self) -> Result<&Self>
pub async fn reload(&self) -> Result<&Self>
Reloads given page
To reload ignoring cache run:
page.execute(ReloadParams::builder().ignore_cache(true).build()).await?;
page.wait_for_navigation().await?;
Sourcepub async fn enable_log(&self) -> Result<&Self>
pub async fn enable_log(&self) -> Result<&Self>
Enables log domain. Enabled by default.
Sends the entries collected so far to the client by means of the entryAdded notification.
See https://chromedevtools.github.io/devtools-protocol/tot/Log#method-enable
Sourcepub async fn disable_log(&self) -> Result<&Self>
pub async fn disable_log(&self) -> Result<&Self>
Disables log domain
Prevents further log entries from being reported to the client
See https://chromedevtools.github.io/devtools-protocol/tot/Log#method-disable
Sourcepub async fn enable_runtime(&self) -> Result<&Self>
pub async fn enable_runtime(&self) -> Result<&Self>
Enables runtime domain. Activated by default.
Sourcepub async fn disable_runtime(&self) -> Result<&Self>
pub async fn disable_runtime(&self) -> Result<&Self>
Disables runtime domain
Sourcepub async fn enable_debugger(&self) -> Result<&Self>
pub async fn enable_debugger(&self) -> Result<&Self>
Enables Debugger. Enabled by default.
Sourcepub async fn disable_debugger(&self) -> Result<&Self>
pub async fn disable_debugger(&self) -> Result<&Self>
Disables Debugger.
pub async fn enable_dom(&self) -> Result<&Self>
pub async fn disable_dom(&self) -> Result<&Self>
pub async fn enable_css(&self) -> Result<&Self>
pub async fn disable_css(&self) -> Result<&Self>
Returns all cookies that match the tab’s current URL.
Set a single cookie
This fails if the cookie’s url or if not provided, the page’s url is
about:blank
or a data:
url.
§Example
page.set_cookie(CookieParam::new("Cookie-name", "Cookie-value")).await?;
Set all the cookies
Delete a single cookie
Delete all the cookies
Sourcepub async fn metrics(&self) -> Result<Vec<Metric>>
pub async fn metrics(&self) -> Result<Vec<Metric>>
Retrieve current values of run-time metrics.
Sourcepub async fn layout_metrics(&self) -> Result<GetLayoutMetricsReturns>
pub async fn layout_metrics(&self) -> Result<GetLayoutMetricsReturns>
Returns metrics relating to the layout of the page
Sourcepub async fn evaluate_expression(
&self,
evaluate: impl Into<EvaluateParams>,
) -> Result<EvaluationResult>
pub async fn evaluate_expression( &self, evaluate: impl Into<EvaluateParams>, ) -> Result<EvaluationResult>
This evaluates strictly as expression.
Same as Page::evaluate
but no fallback or any attempts to detect
whether the expression is actually a function. However you can
submit a function evaluation string:
§Example Evaluate function call as expression
This will take the arguments (1,2)
and will call the function
let sum: usize = page
.evaluate_expression("((a,b) => {return a + b;})(1,2)")
.await?
.into_value()?;
assert_eq!(sum, 3);
Sourcepub async fn evaluate(
&self,
evaluate: impl Into<Evaluation>,
) -> Result<EvaluationResult>
pub async fn evaluate( &self, evaluate: impl Into<Evaluation>, ) -> Result<EvaluationResult>
Evaluates an expression or function in the page’s context and returns the result.
In contrast to Page::evaluate_expression
this is capable of handling
function calls and expressions alike. This takes anything that is
Into<Evaluation>
. When passing a String
or str
, this will try to
detect whether it is a function or an expression. JS function detection
is not very sophisticated but works for general cases ((async) functions
and arrow functions). If you want a string statement
specifically evaluated as expression or function either use the
designated functions Page::evaluate_function
or
Page::evaluate_expression
or use the proper parameter type for
Page::execute
: EvaluateParams
for strict expression evaluation or
CallFunctionOnParams
for strict function evaluation.
If you don’t trust the js function detection and are not sure whether
the statement is an expression or of type function (arrow functions: () => {..}
), you should pass it as EvaluateParams
and set the
EvaluateParams::eval_as_function_fallback
option. This will first
try to evaluate it as expression and if the result comes back
evaluated as RemoteObjectType::Function
it will submit the
statement again but as function:
§Example Evaluate function statement as expression with fallback
option
let eval = EvaluateParams::builder().expression("() => {return 42;}");
// this will fail because the `EvaluationResult` returned by the browser will be
// of type `Function`
let result = page
.evaluate(eval.clone().build().unwrap())
.await?;
assert_eq!(result.object().r#type, RemoteObjectType::Function);
assert!(result.into_value::<usize>().is_err());
// This will also fail on the first try but it detects that the browser evaluated the
// statement as function and then evaluate it again but as function
let sum: usize = page
.evaluate(eval.eval_as_function_fallback(true).build().unwrap())
.await?
.into_value()?;
§Example Evaluate basic expression
let sum:usize = page.evaluate("1 + 2").await?.into_value()?;
assert_eq!(sum, 3);
Sourcepub async fn evaluate_function(
&self,
evaluate: impl Into<CallFunctionOnParams>,
) -> Result<EvaluationResult>
pub async fn evaluate_function( &self, evaluate: impl Into<CallFunctionOnParams>, ) -> Result<EvaluationResult>
Eexecutes a function withinthe page’s context and returns the result.
§Example Evaluate a promise
This will wait until the promise resolves and then returns the result.
let sum:usize = page.evaluate_function("() => Promise.resolve(1 + 2)").await?.into_value()?;
assert_eq!(sum, 3);
§Example Evaluate an async function
let val:usize = page.evaluate_function("async function() {return 42;}").await?.into_value()?;
assert_eq!(val, 42);
§Example Construct a function call
let call = CallFunctionOnParams::builder()
.function_declaration(
"(a,b) => { return a + b;}"
)
.argument(
CallArgument::builder()
.value(serde_json::json!(1))
.build(),
)
.argument(
CallArgument::builder()
.value(serde_json::json!(2))
.build(),
)
.build()
.unwrap();
let sum:usize = page.evaluate_function(call).await?.into_value()?;
assert_eq!(sum, 3);
Sourcepub async fn execution_context(&self) -> Result<Option<ExecutionContextId>>
pub async fn execution_context(&self) -> Result<Option<ExecutionContextId>>
Returns the default execution context identifier of this page that represents the context for JavaScript execution.
Sourcepub async fn secondary_execution_context(
&self,
) -> Result<Option<ExecutionContextId>>
pub async fn secondary_execution_context( &self, ) -> Result<Option<ExecutionContextId>>
Returns the secondary execution context identifier of this page that represents the context for JavaScript execution for manipulating the DOM.
See Page::set_contents
pub async fn frame_execution_context( &self, frame_id: FrameId, ) -> Result<Option<ExecutionContextId>>
pub async fn frame_secondary_execution_context( &self, frame_id: FrameId, ) -> Result<Option<ExecutionContextId>>
Sourcepub async fn evaluate_on_new_document(
&self,
script: impl Into<AddScriptToEvaluateOnNewDocumentParams>,
) -> Result<ScriptIdentifier>
pub async fn evaluate_on_new_document( &self, script: impl Into<AddScriptToEvaluateOnNewDocumentParams>, ) -> Result<ScriptIdentifier>
Evaluates given script in every frame upon creation (before loading frame’s scripts)
Sourcepub async fn set_content(&self, html: impl AsRef<str>) -> Result<&Self>
pub async fn set_content(&self, html: impl AsRef<str>) -> Result<&Self>
Set the content of the frame.
§Example
page.set_content("<body>
<h1>This was set via chromiumoxide</h1>
</body>").await?;
Sourcepub async fn content_bytes(&self) -> Result<Bytes>
pub async fn content_bytes(&self) -> Result<Bytes>
Returns the HTML content of the page
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Page
impl !RefUnwindSafe for Page
impl Send for Page
impl Sync for Page
impl Unpin for Page
impl !UnwindSafe for Page
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more