forked from mattsse/chromiumoxide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.rs
40 lines (32 loc) · 1.1 KB
/
screenshot.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
30
31
32
33
34
35
36
37
38
39
40
use futures::StreamExt;
use chromiumoxide::browser::{Browser, BrowserConfig};
use chromiumoxide::page::ScreenshotParams;
use chromiumoxide_cdp::cdp::browser_protocol::page::CaptureScreenshotFormat;
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let (browser, mut handler) = Browser::launch(BrowserConfig::builder().build()?).await?;
let handle = async_std::task::spawn(async move {
loop {
let _ = handler.next().await.unwrap();
}
});
let page = browser.new_page("https://news.ycombinator.com/").await?;
// take a screenshot of the page
page.save_screenshot(
ScreenshotParams::builder()
.format(CaptureScreenshotFormat::Png)
.full_page(true)
.omit_background(true)
.build(),
"hn-page.png",
)
.await?;
// get the top post and save a screenshot of it
page.find_element("table.itemlist tr")
.await?
.save_screenshot(CaptureScreenshotFormat::Png, "top-post.png")
.await?;
handle.await;
Ok(())
}