pub struct Browser { /* private fields */ }Expand description
A browser instance = 1 chrome.exe process + CDP connection.
Implementations§
Source§impl Browser
impl Browser
Sourcepub fn builder() -> BrowserBuilder
pub fn builder() -> BrowserBuilder
Create a builder for fine-grained control.
Examples found in repository?
examples/profile_reuse.rs (line 16)
11async fn main() {
12 println!("=== Profile Reuse Demo ===\n");
13
14 // --- Session 1: first visit ---
15 println!("[Session 1] First visit with profile({}, {})", PROFILE_INDEX, SEED);
16 let browser = Browser::builder()
17 .headful()
18 .profile(PROFILE_INDEX, SEED)
19 .build().await
20 .expect("failed to create browser");
21
22 let page = browser.navigate("https://www.youtube.com").await
23 .expect("navigate failed");
24 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
25
26 let title = page.js("document.title").await.unwrap_or_default();
27 let cookies = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
28 let fingerprint = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
29
30 println!(" Title: {}", title);
31 println!(" Fingerprint: {}", fingerprint);
32 println!(" Cookies: {} total", cookies.len());
33 for c in cookies.iter().take(5) {
34 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
35 }
36
37 println!(" Shutting down session 1...\n");
38 browser.shutdown().await.expect("shutdown failed");
39
40 // --- Session 2: same profile, cookies should persist ---
41 println!("[Session 2] Reusing profile({}, {}) — cookies should persist", PROFILE_INDEX, SEED);
42 let browser = Browser::builder()
43 .headful()
44 .profile(PROFILE_INDEX, SEED)
45 .build().await
46 .expect("failed to create browser");
47
48 // Check cookies BEFORE navigating — they should be in the user-data-dir
49 let page = browser.navigate("https://www.youtube.com").await
50 .expect("navigate failed");
51 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
52
53 let cookies2 = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
54 let fingerprint2 = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
55
56 println!(" Fingerprint: {}", fingerprint2);
57 println!(" Cookies: {} total", cookies2.len());
58 for c in cookies2.iter().take(5) {
59 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
60 }
61
62 // Verify
63 println!("\n--- Verification ---");
64 println!(" Fingerprint match: {}", fingerprint == fingerprint2);
65 println!(" Cookies persisted: {}", cookies2.len() >= cookies.len());
66
67 browser.shutdown().await.expect("shutdown failed");
68 println!("\n=== DONE ===");
69}Navigate the current page to a URL.
Examples found in repository?
examples/profile_reuse.rs (line 22)
11async fn main() {
12 println!("=== Profile Reuse Demo ===\n");
13
14 // --- Session 1: first visit ---
15 println!("[Session 1] First visit with profile({}, {})", PROFILE_INDEX, SEED);
16 let browser = Browser::builder()
17 .headful()
18 .profile(PROFILE_INDEX, SEED)
19 .build().await
20 .expect("failed to create browser");
21
22 let page = browser.navigate("https://www.youtube.com").await
23 .expect("navigate failed");
24 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
25
26 let title = page.js("document.title").await.unwrap_or_default();
27 let cookies = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
28 let fingerprint = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
29
30 println!(" Title: {}", title);
31 println!(" Fingerprint: {}", fingerprint);
32 println!(" Cookies: {} total", cookies.len());
33 for c in cookies.iter().take(5) {
34 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
35 }
36
37 println!(" Shutting down session 1...\n");
38 browser.shutdown().await.expect("shutdown failed");
39
40 // --- Session 2: same profile, cookies should persist ---
41 println!("[Session 2] Reusing profile({}, {}) — cookies should persist", PROFILE_INDEX, SEED);
42 let browser = Browser::builder()
43 .headful()
44 .profile(PROFILE_INDEX, SEED)
45 .build().await
46 .expect("failed to create browser");
47
48 // Check cookies BEFORE navigating — they should be in the user-data-dir
49 let page = browser.navigate("https://www.youtube.com").await
50 .expect("navigate failed");
51 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
52
53 let cookies2 = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
54 let fingerprint2 = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
55
56 println!(" Fingerprint: {}", fingerprint2);
57 println!(" Cookies: {} total", cookies2.len());
58 for c in cookies2.iter().take(5) {
59 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
60 }
61
62 // Verify
63 println!("\n--- Verification ---");
64 println!(" Fingerprint match: {}", fingerprint == fingerprint2);
65 println!(" Cookies persisted: {}", cookies2.len() >= cookies.len());
66
67 browser.shutdown().await.expect("shutdown failed");
68 println!("\n=== DONE ===");
69}Get cookies for a URL (or all if empty).
Examples found in repository?
examples/profile_reuse.rs (line 27)
11async fn main() {
12 println!("=== Profile Reuse Demo ===\n");
13
14 // --- Session 1: first visit ---
15 println!("[Session 1] First visit with profile({}, {})", PROFILE_INDEX, SEED);
16 let browser = Browser::builder()
17 .headful()
18 .profile(PROFILE_INDEX, SEED)
19 .build().await
20 .expect("failed to create browser");
21
22 let page = browser.navigate("https://www.youtube.com").await
23 .expect("navigate failed");
24 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
25
26 let title = page.js("document.title").await.unwrap_or_default();
27 let cookies = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
28 let fingerprint = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
29
30 println!(" Title: {}", title);
31 println!(" Fingerprint: {}", fingerprint);
32 println!(" Cookies: {} total", cookies.len());
33 for c in cookies.iter().take(5) {
34 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
35 }
36
37 println!(" Shutting down session 1...\n");
38 browser.shutdown().await.expect("shutdown failed");
39
40 // --- Session 2: same profile, cookies should persist ---
41 println!("[Session 2] Reusing profile({}, {}) — cookies should persist", PROFILE_INDEX, SEED);
42 let browser = Browser::builder()
43 .headful()
44 .profile(PROFILE_INDEX, SEED)
45 .build().await
46 .expect("failed to create browser");
47
48 // Check cookies BEFORE navigating — they should be in the user-data-dir
49 let page = browser.navigate("https://www.youtube.com").await
50 .expect("navigate failed");
51 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
52
53 let cookies2 = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
54 let fingerprint2 = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
55
56 println!(" Fingerprint: {}", fingerprint2);
57 println!(" Cookies: {} total", cookies2.len());
58 for c in cookies2.iter().take(5) {
59 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
60 }
61
62 // Verify
63 println!("\n--- Verification ---");
64 println!(" Fingerprint match: {}", fingerprint == fingerprint2);
65 println!(" Cookies persisted: {}", cookies2.len() >= cookies.len());
66
67 browser.shutdown().await.expect("shutdown failed");
68 println!("\n=== DONE ===");
69}Sourcepub async fn screenshot(&self) -> Result<Vec<u8>>
pub async fn screenshot(&self) -> Result<Vec<u8>>
Take a screenshot (PNG bytes).
Sourcepub async fn shutdown(self) -> Result<()>
pub async fn shutdown(self) -> Result<()>
Shut down the browser process cleanly.
Examples found in repository?
examples/profile_reuse.rs (line 38)
11async fn main() {
12 println!("=== Profile Reuse Demo ===\n");
13
14 // --- Session 1: first visit ---
15 println!("[Session 1] First visit with profile({}, {})", PROFILE_INDEX, SEED);
16 let browser = Browser::builder()
17 .headful()
18 .profile(PROFILE_INDEX, SEED)
19 .build().await
20 .expect("failed to create browser");
21
22 let page = browser.navigate("https://www.youtube.com").await
23 .expect("navigate failed");
24 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
25
26 let title = page.js("document.title").await.unwrap_or_default();
27 let cookies = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
28 let fingerprint = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
29
30 println!(" Title: {}", title);
31 println!(" Fingerprint: {}", fingerprint);
32 println!(" Cookies: {} total", cookies.len());
33 for c in cookies.iter().take(5) {
34 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
35 }
36
37 println!(" Shutting down session 1...\n");
38 browser.shutdown().await.expect("shutdown failed");
39
40 // --- Session 2: same profile, cookies should persist ---
41 println!("[Session 2] Reusing profile({}, {}) — cookies should persist", PROFILE_INDEX, SEED);
42 let browser = Browser::builder()
43 .headful()
44 .profile(PROFILE_INDEX, SEED)
45 .build().await
46 .expect("failed to create browser");
47
48 // Check cookies BEFORE navigating — they should be in the user-data-dir
49 let page = browser.navigate("https://www.youtube.com").await
50 .expect("navigate failed");
51 tokio::time::sleep(std::time::Duration::from_secs(3)).await;
52
53 let cookies2 = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
54 let fingerprint2 = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
55
56 println!(" Fingerprint: {}", fingerprint2);
57 println!(" Cookies: {} total", cookies2.len());
58 for c in cookies2.iter().take(5) {
59 println!(" {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
60 }
61
62 // Verify
63 println!("\n--- Verification ---");
64 println!(" Fingerprint match: {}", fingerprint == fingerprint2);
65 println!(" Cookies persisted: {}", cookies2.len() >= cookies.len());
66
67 browser.shutdown().await.expect("shutdown failed");
68 println!("\n=== DONE ===");
69}Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Browser
impl !RefUnwindSafe for Browser
impl Send for Browser
impl Sync for Browser
impl Unpin for Browser
impl UnsafeUnpin for Browser
impl !UnwindSafe for Browser
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
Mutably borrows from an owned value. Read more