pub struct Element<'a> {
    pub remote_object_id: String,
    pub backend_node_id: NodeId,
    pub node_id: NodeId,
    pub parent: &'a Tab,
    pub attributes: Option<Vec<String>>,
    pub tag_name: String,
    pub value: String,
}
Expand description

A handle to a DOM Element.

Typically you get access to these by passing Tab.wait_for_element a CSS selector. Once you have a handle to an element, you can click it, type into it, inspect its attributes, and more. You can even run a JavaScript function inside the tab which can reference the element via this.

Fields§

§remote_object_id: String§backend_node_id: NodeId§node_id: NodeId§parent: &'a Tab§attributes: Option<Vec<String>>§tag_name: String§value: String

Implementations§

source§

impl<'a> Element<'a>

source

pub fn new(parent: &'a Tab, node_id: NodeId) -> Result<Self>

Using a ‘node_id’, of the type returned by QuerySelector and QuerySelectorAll, this finds the ‘backend_node_id’ and ‘remote_object_id’ which are stable identifiers, unlike node_id. We use these two when making various calls to the API because of that.

source

pub fn find_element(&self, selector: &str) -> Result<Self>

Returns the first element in the document which matches the given CSS selector.

Equivalent to the following JS:

document.querySelector(selector)
use headless_chrome::Browser;

let browser = Browser::default()?;
let initial_tab = browser.new_tab()?;

let file_server = server::Server::with_dumb_html(include_str!("../../../../tests/simple.html"));
let containing_element = initial_tab.navigate_to(&file_server.url())?
    .wait_until_navigated()?
    .find_element("div#position-test")?;
let inner_element = containing_element.find_element("#strictly-above")?;
let attrs = inner_element.get_attributes()?.unwrap();
assert_eq!(attrs["id"], "strictly-above");
source

pub fn find_element_by_xpath(&self, query: &str) -> Result<Element<'_>>

source

pub fn find_elements(&self, selector: &str) -> Result<Vec<Self>>

Returns the first element in the document which matches the given CSS selector.

Equivalent to the following JS:

document.querySelector(selector)
use headless_chrome::Browser;

let browser = Browser::default()?;
let initial_tab = browser.new_tab()?;

let file_server = server::Server::with_dumb_html(include_str!("../../../../tests/simple.html"));
let containing_element = initial_tab.navigate_to(&file_server.url())?
    .wait_until_navigated()?
    .find_element("div#position-test")?;
let inner_divs = containing_element.find_elements("div")?;
assert_eq!(inner_divs.len(), 5);
source

pub fn find_elements_by_xpath(&self, query: &str) -> Result<Vec<Element<'_>>>

source

pub fn wait_for_element(&self, selector: &str) -> Result<Element<'_>>

source

pub fn wait_for_xpath(&self, selector: &str) -> Result<Element<'_>>

source

pub fn wait_for_element_with_custom_timeout( &self, selector: &str, timeout: Duration ) -> Result<Element<'_>>

source

pub fn wait_for_xpath_with_custom_timeout( &self, selector: &str, timeout: Duration ) -> Result<Element<'_>>

source

pub fn wait_for_elements(&self, selector: &str) -> Result<Vec<Element<'_>>>

source

pub fn wait_for_elements_by_xpath( &self, selector: &str ) -> Result<Vec<Element<'_>>>

source

pub fn move_mouse_over(&self) -> Result<&Self>

Moves the mouse to the middle of this element

source

pub fn click(&self) -> Result<&Self>

source

pub fn type_into(&self, text: &str) -> Result<&Self>

source

pub fn call_js_fn( &self, function_declaration: &str, args: Vec<Value>, await_promise: bool ) -> Result<RemoteObject>

source

pub fn focus(&self) -> Result<&Self>

source

pub fn get_inner_text(&self) -> Result<String>

Returns the inner text of an HTML Element. Returns an empty string on elements with no text.

Note: .innerText and .textContent are not the same thing. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText

Note: if you somehow call this on a node that’s not an HTML Element (e.g. document), this will fail.

use headless_chrome::Browser;
use std::time::Duration;
let browser = Browser::default()?;
let url = "https://web.archive.org/web/20190403224553/https://en.wikipedia.org/wiki/JavaScript";
let inner_text_content = browser.new_tab()?
    .navigate_to(url)?
    .wait_for_element_with_custom_timeout("#Misplaced_trust_in_developers", Duration::from_secs(10))?
    .get_inner_text()?;
assert_eq!(inner_text_content, "Misplaced trust in developers");
source

pub fn get_content(&self) -> Result<String>

Get the full HTML contents of the element.

Equivalent to the following JS: element.outerHTML.

source

pub fn get_computed_styles(&self) -> Result<Vec<CSSComputedStyleProperty>>

source

pub fn get_description(&self) -> Result<Node>

source

pub fn capture_screenshot( &self, format: CaptureScreenshotFormatOption ) -> Result<Vec<u8>>

Capture a screenshot of this element.

The screenshot is taken from the surface using this element’s content-box.

use headless_chrome::{protocol::page::ScreenshotFormat, Browser};
let browser = Browser::default()?;
let png_data = browser.new_tab()?
    .navigate_to("https://en.wikipedia.org/wiki/WebKit")?
    .wait_for_element("#mw-content-text > div > table.infobox.vevent")?
    .capture_screenshot(ScreenshotFormat::PNG)?;
source

pub fn set_input_files(&self, file_paths: &[&str]) -> Result<&Self>

source

pub fn scroll_into_view(&self) -> Result<&Self>

Scrolls the current element into view

Used prior to any action applied to the current element to ensure action is duable.

source

pub fn get_attributes(&self) -> Result<Option<Vec<String>>>

source

pub fn get_attribute_value( &self, attribute_name: &str ) -> Result<Option<String>>

source

pub fn get_box_model(&self) -> Result<BoxModel>

Get boxes for this element

source

pub fn get_midpoint(&self) -> Result<Point>

source

pub fn get_js_midpoint(&self) -> Result<Point>

Trait Implementations§

source§

impl<'a> Debug for Element<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Element<'a>

§

impl<'a> Send for Element<'a>

§

impl<'a> Sync for Element<'a>

§

impl<'a> Unpin for Element<'a>

§

impl<'a> !UnwindSafe for Element<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V