forked from rust-headless-chrome/rust-headless-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_wikipedia.rs
29 lines (26 loc) · 916 Bytes
/
query_wikipedia.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use anyhow::Result;
use headless_chrome::{Browser, LaunchOptions};
fn query(input: &str) -> Result<()> {
let browser = Browser::new(
LaunchOptions::default_builder()
.build()
.expect("Could not find chrome-executable"),
)?;
let tab = browser.new_tab()?;
tab.navigate_to("https://en.wikipedia.org")?
.wait_for_element("input#searchInput")?
.click()?;
tab.type_str(input)?.press_key("Enter")?;
match tab.wait_for_element("div.shortdescription") {
Err(e) => eprintln!("Query failed: {e:?}"),
Ok(e) => match e.get_description()?.find(|n| n.node_name == "#text") {
Some(n) => println!("Result for `{}`: {}", &input, n.node_value),
None => eprintln!("No shortdescription-node found on page"),
},
}
Ok(())
}
fn main() -> Result<()> {
let input = "Elvis Aaron Presley";
query(input)
}