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>
impl<'a> Element<'a>
sourcepub fn new(parent: &'a Tab, node_id: NodeId) -> Result<Self>
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.
sourcepub fn find_element(&self, selector: &str) -> Result<Self>
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");
pub fn find_element_by_xpath(&self, query: &str) -> Result<Element<'_>>
sourcepub fn find_elements(&self, selector: &str) -> Result<Vec<Self>>
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);
pub fn find_elements_by_xpath(&self, query: &str) -> Result<Vec<Element<'_>>>
pub fn wait_for_element(&self, selector: &str) -> Result<Element<'_>>
pub fn wait_for_xpath(&self, selector: &str) -> Result<Element<'_>>
pub fn wait_for_element_with_custom_timeout( &self, selector: &str, timeout: Duration ) -> Result<Element<'_>>
pub fn wait_for_xpath_with_custom_timeout( &self, selector: &str, timeout: Duration ) -> Result<Element<'_>>
pub fn wait_for_elements(&self, selector: &str) -> Result<Vec<Element<'_>>>
pub fn wait_for_elements_by_xpath( &self, selector: &str ) -> Result<Vec<Element<'_>>>
sourcepub fn move_mouse_over(&self) -> Result<&Self>
pub fn move_mouse_over(&self) -> Result<&Self>
Moves the mouse to the middle of this element
pub fn click(&self) -> Result<&Self>
pub fn type_into(&self, text: &str) -> Result<&Self>
pub fn call_js_fn( &self, function_declaration: &str, args: Vec<Value>, await_promise: bool ) -> Result<RemoteObject>
pub fn focus(&self) -> Result<&Self>
sourcepub fn get_inner_text(&self) -> Result<String>
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");
sourcepub fn get_content(&self) -> Result<String>
pub fn get_content(&self) -> Result<String>
Get the full HTML contents of the element.
Equivalent to the following JS: element.outerHTML
.
pub fn get_computed_styles(&self) -> Result<Vec<CSSComputedStyleProperty>>
pub fn get_description(&self) -> Result<Node>
sourcepub fn capture_screenshot(
&self,
format: CaptureScreenshotFormatOption
) -> Result<Vec<u8>>
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)?;
pub fn set_input_files(&self, file_paths: &[&str]) -> Result<&Self>
sourcepub fn scroll_into_view(&self) -> Result<&Self>
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.
pub fn get_attributes(&self) -> Result<Option<Vec<String>>>
pub fn get_attribute_value( &self, attribute_name: &str ) -> Result<Option<String>>
sourcepub fn get_box_model(&self) -> Result<BoxModel>
pub fn get_box_model(&self) -> Result<BoxModel>
Get boxes for this element