rpage 1.0.0

A Rust browser automation library inspired by DrissionPage
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
//! rpage 同步封装 — 零 await,像 DrissionPage 一样用
//!
//! ```
//! use rpage::sync::SyncPage;
//!
//! let page = SyncPage::connect("http://127.0.0.1:9222").unwrap();
//! page.get("https://example.com").unwrap();
//! let title = page.title().unwrap();
//! let el = page.ele("h1").unwrap();
//! println!("{}", el.text());
//! el.click().unwrap();
//! ```

use crate::agent::{ActionAttempt, InteractiveElement, PageSnapshot, PageSummary};
use crate::chromium_page::{
    ChromiumPage, CookieInfo, FrameContext, InterceptGuard, InterceptedRequest, PdfOptions,
};
use crate::download::DownloadInfo;
use crate::element::Element;
use crate::error::{Error, Result};
use std::time::Duration;

/// 同步 Page — 内部持有 tokio runtime,所有方法零 await
pub struct SyncPage {
    inner: ChromiumPage,
    rt: tokio::runtime::Runtime,
}

impl SyncPage {
    // ── 构造 ──────────────────────────────────────────

    /// 启动浏览器并接管(同步)
    pub fn new() -> Result<Self> {
        let rt =
            tokio::runtime::Runtime::new().map_err(|e| Error::Browser(format!("runtime: {e}")))?;
        let inner = rt.block_on(ChromiumPage::new())?;
        Ok(Self { inner, rt })
    }

    /// 用自定义选项启动浏览器(同步)
    pub fn with_options(opts: crate::config::ChromiumOptions) -> Result<Self> {
        let rt =
            tokio::runtime::Runtime::new().map_err(|e| Error::Browser(format!("runtime: {e}")))?;
        let inner = rt.block_on(ChromiumPage::with_options(opts))?;
        Ok(Self { inner, rt })
    }

    /// 连接已运行的 Chrome(同步)
    pub fn connect(debug_url: &str) -> Result<Self> {
        let rt =
            tokio::runtime::Runtime::new().map_err(|e| Error::Browser(format!("runtime: {e}")))?;
        let inner = rt.block_on(ChromiumPage::connect(debug_url))?;
        Ok(Self { inner, rt })
    }

    /// 连接已运行的 Chrome(带选项)
    pub fn connect_with_opts(
        debug_url: &str,
        opts: crate::config::ChromiumOptions,
    ) -> Result<Self> {
        let rt =
            tokio::runtime::Runtime::new().map_err(|e| Error::Browser(format!("runtime: {e}")))?;
        let inner = rt.block_on(ChromiumPage::connect_with_opts(debug_url, opts))?;
        Ok(Self { inner, rt })
    }

    // ── 内部辅助 ──────────────────────────────────────

    #[inline]
    fn rt(&self) -> &tokio::runtime::Runtime {
        &self.rt
    }

    // ── 导航 ──────────────────────────────────────────

    pub fn get(&self, url: &str) -> Result<()> {
        self.rt().block_on(self.inner.get(url))
    }
    pub fn goto(&self, url: &str) -> Result<&Self> {
        self.rt().block_on(self.inner.goto(url))?;
        Ok(self)
    }
    pub fn refresh(&self) -> Result<()> {
        self.rt().block_on(self.inner.refresh())
    }
    pub fn back(&self) -> Result<()> {
        self.rt().block_on(self.inner.back())
    }
    pub fn forward(&self) -> Result<()> {
        self.rt().block_on(self.inner.forward())
    }
    pub fn get_and_wait(&self, url: &str, timeout_secs: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.get_and_wait(url, timeout_secs))
    }

    // ── 页面信息 ──────────────────────────────────────

    pub fn title(&self) -> Result<String> {
        self.rt().block_on(self.inner.title())
    }
    pub fn url(&self) -> Result<String> {
        self.rt().block_on(self.inner.url())
    }
    pub fn current_url(&self) -> Result<String> {
        self.rt().block_on(self.inner.current_url())
    }
    pub fn current_title(&self) -> Result<String> {
        self.rt().block_on(self.inner.current_title())
    }
    pub fn html(&self) -> Result<String> {
        self.rt().block_on(self.inner.html())
    }
    pub fn page_source(&self) -> Result<String> {
        self.rt().block_on(self.inner.page_source())
    }
    pub fn content_type(&self) -> Result<String> {
        self.rt().block_on(self.inner.get_content_type())
    }

    // ── 元素查找 ──────────────────────────────────────

    pub fn ele(&self, selector: &str) -> Result<SyncElement> {
        let el = self.rt().block_on(self.inner.ele(selector))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }

    pub fn eles(&self, selector: &str) -> Result<Vec<SyncElement>> {
        let els = self.rt().block_on(self.inner.eles(selector))?;
        let handle = self.rt().handle().clone();
        Ok(els
            .into_iter()
            .map(|e| SyncElement {
                inner: e,
                rt: handle.clone(),
            })
            .collect())
    }

    pub fn s_ele(&self, selector: &str) -> Result<SyncElement> {
        let el = self.rt().block_on(self.inner.s_ele(selector))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }

    pub fn s_eles(&self, selector: &str) -> Result<Vec<SyncElement>> {
        let els = self.rt().block_on(self.inner.s_eles(selector))?;
        let handle = self.rt().handle().clone();
        Ok(els
            .into_iter()
            .map(|e| SyncElement {
                inner: e,
                rt: handle.clone(),
            })
            .collect())
    }

    // ── 语义定位 (Playwright get_by_*) ──
    pub fn get_by_text(&self, text: &str) -> Result<SyncElement> {
        let el = self.rt().block_on(self.inner.get_by_text(text))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }
    pub fn get_by_placeholder(&self, placeholder: &str) -> Result<SyncElement> {
        let el = self
            .rt()
            .block_on(self.inner.get_by_placeholder(placeholder))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }
    pub fn get_by_test_id(&self, test_id: &str) -> Result<SyncElement> {
        let el = self.rt().block_on(self.inner.get_by_test_id(test_id))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }
    pub fn get_by_role(&self, role: &str) -> Result<SyncElement> {
        let el = self.rt().block_on(self.inner.get_by_role(role))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }
    pub fn get_by_label(&self, label: &str) -> Result<SyncElement> {
        let el = self.rt().block_on(self.inner.get_by_label(label))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }

    pub fn ele_or_none(&self, selector: &str) -> Option<SyncElement> {
        self.rt()
            .block_on(self.inner.ele_or_none(selector))
            .map(|e| SyncElement {
                inner: e,
                rt: self.rt().handle().clone(),
            })
    }

    pub fn exists(&self, selector: &str) -> bool {
        self.rt().block_on(self.inner.exists(selector))
    }
    pub fn count(&self, selector: &str) -> usize {
        self.rt().block_on(self.inner.count(selector))
    }
    pub fn ele_count(&self, selector: &str) -> Result<usize> {
        self.rt().block_on(self.inner.ele_count(selector))
    }

    // ── 快捷操作(链式返回 &Self)──────────────────────

    pub fn click_ele(&self, selector: &str) -> Result<&Self> {
        self.rt().block_on(self.inner.click_ele(selector))?;
        Ok(self)
    }
    pub fn type_text(&self, selector: &str, text: &str) -> Result<&Self> {
        self.rt().block_on(self.inner.type_text(selector, text))?;
        Ok(self)
    }
    pub fn input_text(&self, selector: &str, text: &str) -> Result<&Self> {
        self.rt().block_on(self.inner.input_text(selector, text))?;
        Ok(self)
    }
    pub fn hover_ele(&self, selector: &str) -> Result<&Self> {
        self.rt().block_on(self.inner.hover_ele(selector))?;
        Ok(self)
    }
    pub fn scroll_to_ele(&self, selector: &str) -> Result<&Self> {
        self.rt().block_on(self.inner.scroll_to_ele(selector))?;
        Ok(self)
    }
    pub fn get_text(&self, selector: &str) -> Result<String> {
        self.rt().block_on(self.inner.get_text(selector))
    }
    pub fn get_attr(&self, selector: &str, attr: &str) -> Result<Option<String>> {
        self.rt().block_on(self.inner.get_attr(selector, attr))
    }

    // ── JS 执行 ───────────────────────────────────────

    pub fn execute(&self, js: &str) -> Result<serde_json::Value> {
        self.rt().block_on(self.inner.execute(js))
    }
    pub fn run_async_js(&self, expr: &str) -> Result<serde_json::Value> {
        self.rt().block_on(self.inner.run_async_js(expr))
    }
    pub fn run_js_with_args(
        &self,
        expr: &str,
        args: serde_json::Value,
    ) -> Result<serde_json::Value> {
        self.rt().block_on(self.inner.run_js_with_args(expr, args))
    }
    pub fn evaluate_on_new_document(&self, js: &str) -> Result<()> {
        self.rt().block_on(self.inner.evaluate_on_new_document(js))
    }
    pub fn add_init_script(&self, name: &str, js: &str) -> Result<()> {
        self.rt().block_on(self.inner.add_init_script(name, js))
    }
    pub fn remove_init_script(&self, name: &str) -> Result<()> {
        self.rt().block_on(self.inner.remove_init_script(name))
    }

    // ── 等待 ──────────────────────────────────────────

    pub fn wait_ele(&self, selector: &str, timeout_secs: u64) -> Result<SyncElement> {
        let el = self
            .rt()
            .block_on(self.inner.wait_ele(selector, timeout_secs))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt().handle().clone(),
        })
    }
    pub fn wait_ele_hidden(&self, selector: &str, timeout_secs: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.wait_ele_hidden(selector, timeout_secs))
    }
    pub fn wait_ele_deleted(&self, selector: &str, timeout_secs: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.wait_ele_deleted(selector, timeout_secs))
    }
    pub fn wait_title_contains(&self, text: &str, timeout_secs: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.wait_title_contains(text, timeout_secs))
    }
    pub fn wait_url_contains(&self, text: &str, timeout_secs: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.wait_url_contains(text, timeout_secs))
    }
    pub fn wait_url_is(&self, expected: &str, timeout_secs: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.wait_url_is(expected, timeout_secs))
    }
    pub fn wait_title_is(&self, expected: &str, timeout_secs: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.wait_title_is(expected, timeout_secs))
    }
    pub fn wait_js(&self, expr: &str, timeout_secs: u64) -> Result<()> {
        self.rt().block_on(self.inner.wait_js(expr, timeout_secs))
    }
    pub fn wait_for_navigation(&self, expected_url: &str, timeout_secs: u64) -> Result<()> {
        self.rt().block_on(
            self.inner
                .wait_for_navigation(expected_url, std::time::Duration::from_secs(timeout_secs)),
        )
    }
    pub fn wait_new_tab(&self, timeout_secs: u64) -> Result<()> {
        self.rt().block_on(self.inner.wait_new_tab(timeout_secs))
    }
    pub fn wait_download(&self, timeout_secs: u64) -> Result<DownloadInfo> {
        self.rt().block_on(self.inner.wait_download(timeout_secs))
    }

    // ── 截图 / PDF ────────────────────────────────────

    pub fn screenshot_bytes(&self) -> Result<Vec<u8>> {
        self.rt().block_on(self.inner.screenshot_bytes())
    }
    pub fn screenshot(&self, path: &str) -> Result<()> {
        self.rt().block_on(self.inner.screenshot(path))
    }
    pub fn pdf(&self, path: &str) -> Result<()> {
        self.rt().block_on(self.inner.pdf(path))
    }
    pub fn pdf_bytes(&self, opts: PdfOptions) -> Result<Vec<u8>> {
        self.rt().block_on(self.inner.pdf_bytes(opts))
    }
    pub fn pdf_to_file(&self, path: &str, opts: PdfOptions) -> Result<()> {
        self.rt().block_on(self.inner.pdf_to_file(path, opts))
    }

    // ── Cookie ────────────────────────────────────────

    pub fn cookies(&self) -> Result<Vec<CookieInfo>> {
        self.rt().block_on(self.inner.cookies())
    }
    pub fn set_cookie(&self, cookie: CookieInfo) -> Result<()> {
        self.rt().block_on(self.inner.set_cookie(cookie))
    }
    pub fn delete_cookie(&self, name: &str) -> Result<()> {
        self.rt().block_on(self.inner.delete_cookie(name))
    }
    pub fn clear_cookies(&self) -> Result<()> {
        self.rt().block_on(self.inner.clear_cookies())
    }
    pub fn share_cookies_to(&self, other: &SyncPage) -> Result<()> {
        self.rt()
            .block_on(self.inner.share_cookies_to(&other.inner))
    }

    // ── 标签页 ────────────────────────────────────────

    /// Number of open tabs. (A `Vec<SyncPage>` isn't returnable: each SyncPage
    /// owns its own runtime and chromiumoxide's tab handles aren't
    /// `ChromiumPage`s — drive multiple tabs with `tab_titles`/`tab_urls` +
    /// `switch_to_tab`/`close_tab` instead.) Replaces the old `tabs()`, which
    /// could only ever return an `Err` with the titles stuffed in its message.
    pub fn tab_count(&self) -> Result<usize> {
        Ok(self.rt().block_on(self.inner.tab_titles())?.len())
    }
    pub fn tab_titles(&self) -> Result<Vec<String>> {
        self.rt().block_on(self.inner.tab_titles())
    }
    pub fn tab_urls(&self) -> Result<Vec<String>> {
        self.rt().block_on(self.inner.tab_urls())
    }
    pub fn switch_to_tab(&self, index: usize) -> Result<()> {
        self.rt().block_on(self.inner.switch_to_tab(index))
    }
    pub fn close_tab(&self, index: usize) -> Result<()> {
        self.rt().block_on(self.inner.close_tab(index))
    }
    pub fn new_tab(&self) -> Result<()> {
        self.rt().block_on(self.inner.new_tab())?;
        Ok(())
    }
    pub fn get_tab_by_title(&self, title: &str) -> Result<usize> {
        self.rt().block_on(self.inner.get_tab_by_title(title))
    }
    pub fn get_tab_by_url(&self, url: &str) -> Result<usize> {
        self.rt().block_on(self.inner.get_tab_by_url(url))
    }

    // ── 滚动 ──────────────────────────────────────────

    pub fn scroll_to(&self, x: u32, y: u32) -> Result<()> {
        self.rt().block_on(self.inner.scroll_to(x, y))
    }
    pub fn scroll_to_top(&self) -> Result<()> {
        self.rt().block_on(self.inner.scroll_to_top())
    }
    pub fn scroll_to_bottom(&self) -> Result<()> {
        self.rt().block_on(self.inner.scroll_to_bottom())
    }
    pub fn scroll_up(&self, pixels: u32) -> Result<()> {
        self.rt().block_on(self.inner.scroll_up(pixels))
    }
    pub fn scroll_down(&self, pixels: u32) -> Result<()> {
        self.rt().block_on(self.inner.scroll_down(pixels))
    }
    pub fn scroll_by(&self, x: i64, y: i64) -> Result<()> {
        self.rt().block_on(self.inner.scroll_by(x, y))
    }
    pub fn smooth_scroll(&self, x: i64, y: i64, duration_ms: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.smooth_scroll(x, y, duration_ms))
    }

    // ── 键盘 ──────────────────────────────────────────

    pub fn press(&self, key: &str) -> Result<()> {
        self.rt().block_on(self.inner.press(key))
    }
    pub fn keys(&self, text: &str) -> Result<()> {
        self.rt().block_on(self.inner.keys(text))
    }

    // ── 网络 ──────────────────────────────────────────

    pub fn set_extra_headers(
        &self,
        headers: std::collections::HashMap<String, String>,
    ) -> Result<()> {
        self.rt().block_on(self.inner.set_extra_headers(headers))
    }
    pub fn set_user_agent(&self, ua: &str) -> Result<()> {
        self.rt().block_on(self.inner.set_user_agent(ua))
    }
    pub fn set_proxy_auth(&self, user: &str, pass: &str) -> Result<()> {
        self.rt().block_on(self.inner.set_proxy_auth(user, pass))
    }
    pub fn set_blocked_urls(&self, urls: &[&str]) -> Result<()> {
        self.rt().block_on(self.inner.set_blocked_urls(urls))
    }
    pub fn set_offline(&self, offline: bool) -> Result<()> {
        self.rt().block_on(self.inner.set_offline(offline))
    }
    pub fn clear_cache(&self) -> Result<()> {
        self.rt().block_on(self.inner.clear_cache())
    }
    pub fn run_cdp(&self, method: &str, params: serde_json::Value) -> Result<serde_json::Value> {
        self.rt().block_on(self.inner.run_cdp(method, params))
    }
    pub fn get_response_body(
        &self,
        request_id: &str,
    ) -> Result<crate::chromium_page::ResponseBody> {
        self.rt().block_on(self.inner.get_response_body(request_id))
    }
    pub fn wait_data_packet(
        &self,
        url_pattern: &str,
        timeout_secs: u64,
    ) -> Result<crate::chromium_page::DataPacket> {
        self.rt()
            .block_on(self.inner.wait_data_packet(url_pattern, timeout_secs))
    }
    pub fn data_packets(&self, url_pattern: &str) -> Vec<crate::chromium_page::DataPacket> {
        self.rt().block_on(self.inner.data_packets(url_pattern))
    }
    pub fn listen_start(&self) -> Result<()> {
        self.rt().block_on(self.inner.listen_start())
    }
    pub fn listen_stop(&self) -> Result<()> {
        self.rt().block_on(self.inner.listen_stop())
    }
    pub fn get_packets(&self, url_pattern: &str) -> Vec<crate::network::RequestInfo> {
        self.inner.get_packets(url_pattern)
    }
    pub fn get_responses(&self, url_pattern: &str) -> Vec<crate::network::ResponseInfo> {
        self.inner.get_responses(url_pattern)
    }
    pub fn wait_for_packet(
        &self,
        url_pattern: &str,
        timeout_secs: u64,
    ) -> Result<crate::network::RequestInfo> {
        self.inner.wait_for_packet(url_pattern, timeout_secs)
    }
    pub fn wait_network_idle(&self, timeout_secs: u64, quiet_ms: u64) -> Result<()> {
        self.rt()
            .block_on(self.inner.wait_network_idle(timeout_secs, quiet_ms))
    }
    pub fn links(&self) -> Result<Vec<String>> {
        self.rt().block_on(self.inner.links())
    }
    pub fn images(&self) -> Result<Vec<String>> {
        self.rt().block_on(self.inner.images())
    }
    pub fn disable_images(&self) -> Result<()> {
        self.rt().block_on(self.inner.disable_images())
    }
    pub fn download(&self, url: &str, timeout_secs: u64) -> Result<DownloadInfo> {
        self.rt().block_on(self.inner.download(url, timeout_secs))
    }

    // ── 视口 / 窗口 / 设备 ────────────────────────────

    pub fn set_viewport(&self, w: u32, h: u32) -> Result<()> {
        self.rt().block_on(self.inner.set_viewport(w, h))
    }
    pub fn set_device_scale(&self, scale: f64) -> Result<()> {
        self.rt().block_on(self.inner.set_device_scale(scale))
    }
    pub fn set_touch(&self, enabled: bool) -> Result<()> {
        self.rt().block_on(self.inner.set_touch(enabled))
    }
    pub fn set_geolocation(&self, lat: f64, lng: f64) -> Result<()> {
        self.rt().block_on(self.inner.set_geolocation(lat, lng))
    }
    pub fn set_timezone(&self, tz: &str) -> Result<()> {
        self.rt().block_on(self.inner.set_timezone(tz))
    }
    pub fn set_window_position(&self, left: i32, top: i32) -> Result<()> {
        self.rt()
            .block_on(self.inner.set_window_position(left, top))
    }
    pub fn set_window_size(&self, w: u32, h: u32) -> Result<()> {
        self.rt().block_on(self.inner.set_window_size(w, h))
    }
    pub fn get_window_bounds(&self) -> Result<(i32, i32, u32, u32)> {
        self.rt().block_on(self.inner.get_window_bounds())
    }
    pub fn minimize(&self) -> Result<()> {
        self.rt().block_on(self.inner.minimize())
    }
    pub fn maximize(&self) -> Result<()> {
        self.rt().block_on(self.inner.maximize())
    }
    pub fn fullscreen(&self) -> Result<()> {
        self.rt().block_on(self.inner.fullscreen())
    }
    pub fn emulate_device(
        &self,
        width: u32,
        height: u32,
        ua: &str,
        scale: f64,
        touch: bool,
    ) -> Result<()> {
        self.rt()
            .block_on(self.inner.emulate_device(width, height, ua, scale, touch))
    }

    // ── 剪贴板 ────────────────────────────────────────

    pub fn clipboard_read(&self) -> Result<String> {
        self.rt().block_on(self.inner.clipboard_read())
    }
    pub fn clipboard_write(&self, text: &str) -> Result<()> {
        self.rt().block_on(self.inner.clipboard_write(text))
    }

    // ── 文本操作 ──────────────────────────────────────

    pub fn select_all_text(&self) -> Result<()> {
        self.rt().block_on(self.inner.select_all_text())
    }
    pub fn copy_text(&self) -> Result<()> {
        self.rt().block_on(self.inner.copy_text())
    }
    pub fn paste_text(&self) -> Result<()> {
        self.rt().block_on(self.inner.paste_text())
    }
    pub fn find_text(&self, text: &str) -> Result<bool> {
        self.rt().block_on(self.inner.find_text(text))
    }

    // ── 弹窗 ──────────────────────────────────────────

    pub fn handle_alert(&self, accept: bool, text: Option<&str>) -> Result<()> {
        self.rt().block_on(self.inner.handle_alert(accept, text))
    }
    pub fn accept_alert(&self) -> Result<()> {
        self.rt().block_on(self.inner.accept_alert())
    }
    pub fn dismiss_alert(&self) -> Result<()> {
        self.rt().block_on(self.inner.dismiss_alert())
    }
    pub fn accept_prompt(&self, text: &str) -> Result<()> {
        self.rt().block_on(self.inner.accept_prompt(text))
    }

    // ── CSS / 样式 ────────────────────────────────────

    pub fn inject_css(&self, css: &str) -> Result<String> {
        self.rt().block_on(self.inner.inject_css(css))
    }
    pub fn remove_css(&self, id: &str) -> Result<()> {
        self.rt().block_on(self.inner.remove_css(id))
    }

    // ── iframe ────────────────────────────────────────

    pub fn frame_html(&self, selector: &str) -> Result<String> {
        self.rt().block_on(self.inner.frame_html(selector))
    }
    pub fn frame_execute(&self, selector: &str, js: &str) -> Result<serde_json::Value> {
        self.rt().block_on(self.inner.frame_execute(selector, js))
    }
    pub fn enter_frame(&self, selector: &str) -> Result<FrameContext> {
        self.rt().block_on(self.inner.enter_frame(selector))
    }

    // ── 性能 ──────────────────────────────────────────

    pub fn performance_metrics(&self) -> Result<Vec<(String, f64)>> {
        self.rt().block_on(self.inner.performance_metrics())
    }
    pub fn page_timing(&self) -> Result<std::collections::HashMap<String, f64>> {
        self.rt().block_on(self.inner.page_timing())
    }
    pub fn dom_snapshot(&self) -> Result<serde_json::Value> {
        self.rt().block_on(self.inner.dom_snapshot())
    }

    // ── 监控查询(console / WebSocket / 下载,均为同步)──
    pub fn console_log(&self) -> Vec<crate::console::ConsoleEntry> {
        self.inner.console_log()
    }
    pub fn console_exceptions(&self) -> Vec<crate::console::JsException> {
        self.inner.console_exceptions()
    }
    pub fn clear_console(&self) {
        self.inner.clear_console()
    }
    pub fn ws_frames(&self) -> Vec<crate::websocket::WsFrame> {
        self.inner.ws_frames()
    }
    pub fn ws_events(&self) -> Vec<crate::websocket::WsEvent> {
        self.inner.ws_events()
    }
    pub fn clear_ws_frames(&self) {
        self.inner.clear_ws_frames()
    }
    pub fn downloads(&self) -> Vec<DownloadInfo> {
        self.inner.downloads()
    }
    pub fn clear_downloads(&self) {
        self.inner.clear_downloads()
    }
    pub fn load_strategy(&self) -> &str {
        self.inner.load_strategy()
    }

    // ── 健壮导航(出错不致命)──
    pub fn safe_back(&self) -> Result<()> {
        self.rt().block_on(self.inner.safe_back())
    }
    pub fn safe_forward(&self) -> Result<()> {
        self.rt().block_on(self.inner.safe_forward())
    }
    pub fn safe_refresh(&self) -> Result<()> {
        self.rt().block_on(self.inner.safe_refresh())
    }

    // ── 权限 / 设备 ───────────────────────────────────

    pub fn grant_permissions(&self, origin: &str, perms: Vec<String>) -> Result<()> {
        self.rt()
            .block_on(self.inner.grant_permissions(origin, perms))
    }
    pub fn reset_permissions(&self) -> Result<()> {
        self.rt().block_on(self.inner.reset_permissions())
    }
    pub fn mute(&self) -> Result<()> {
        self.rt().block_on(self.inner.mute())
    }
    pub fn unmute(&self) -> Result<()> {
        self.rt().block_on(self.inner.unmute())
    }

    // ── 下载 ──────────────────────────────────────────

    pub fn set_download_file_name(&self, name: &str) -> Result<()> {
        self.rt().block_on(self.inner.set_download_file_name(name))
    }
    pub fn wait_for_download_file(&self, dir: &str, timeout_secs: u64) -> Result<DownloadInfo> {
        self.rt()
            .block_on(self.inner.wait_for_download_file(dir, timeout_secs))
    }
    pub fn set_file_chooser(&self, enabled: bool) {
        self.rt().block_on(self.inner.set_file_chooser(enabled))
    }
    pub fn wait_file_chooser(
        &self,
        timeout_secs: u64,
    ) -> Result<crate::chromium_page::FileChooserInfo> {
        self.rt()
            .block_on(self.inner.wait_file_chooser(timeout_secs))
    }

    // ── 网络/位置 ─────────────────────────────────────

    pub fn set_location_and_reload(&self, lat: f64, lng: f64) -> Result<()> {
        self.rt()
            .block_on(self.inner.set_location_and_reload(lat, lng))
    }

    // ── Agent API ─────────────────────────────────────

    pub fn interactive_elements(&self) -> Result<Vec<InteractiveElement>> {
        self.rt().block_on(self.inner.interactive_elements())
    }
    pub fn page_summary(&self) -> Result<PageSummary> {
        self.rt().block_on(self.inner.page_summary())
    }
    pub fn page_snapshot(&self) -> Result<PageSnapshot> {
        self.rt().block_on(self.inner.page_snapshot())
    }
    pub fn smart_click(&self, target: &str) -> ActionAttempt {
        self.rt().block_on(self.inner.smart_click(target))
    }
    pub fn smart_fill(&self, field: &str, value: &str) -> ActionAttempt {
        self.rt().block_on(self.inner.smart_fill(field, value))
    }

    // ── 生命周期 ──────────────────────────────────────

    pub fn sleep(&self, dur: Duration) {
        self.rt().block_on(self.inner.sleep(dur))
    }
    pub fn close(&self) -> Result<()> {
        self.rt().block_on(self.inner.close())
    }
    pub fn quit(&self) -> Result<()> {
        self.rt().block_on(self.inner.quit())
    }
    // reconnect() 需要 &mut self,与 SyncPage 的 &self 模式冲突
    // 如需重连请直接重建 SyncPage::connect()
    pub fn clone_session(&self) -> Result<SyncPage> {
        let inner = self.rt().block_on(self.inner.clone_session())?;
        Ok(SyncPage {
            inner,
            rt: tokio::runtime::Runtime::new()
                .map_err(|e| Error::Browser(format!("runtime: {e}")))?,
        })
    }

    pub fn is_connected(&self) -> bool {
        self.inner.is_connected()
    }
    pub fn debug_url(&self) -> &str {
        self.inner.debug_url()
    }

    // ── 拦截 ──────────────────────────────────────────

    /// Enable Fetch interception and return a *synchronous* guard. Unlike the
    /// raw async `InterceptGuard`, `SyncInterceptGuard` lets sync callers
    /// continue / redirect / fail paused requests without touching a runtime.
    pub fn enable_intercept(&self, pattern: &str) -> Result<SyncInterceptGuard> {
        let inner = self.rt().block_on(self.inner.enable_intercept(pattern))?;
        Ok(SyncInterceptGuard {
            inner,
            rt: self.rt().handle().clone(),
        })
    }

    // ── 刷新元素 ──────────────────────────────────────

    pub fn refresh_ele(&self, el: &SyncElement) -> Result<SyncElement> {
        let refreshed = self.rt().block_on(self.inner.refresh_ele(&el.inner))?;
        Ok(SyncElement {
            inner: refreshed,
            rt: self.rt().handle().clone(),
        })
    }
}

/// 同步 Element — 内部持有 tokio handle,所有方法零 await
pub struct SyncElement {
    inner: Element,
    rt: tokio::runtime::Handle,
}

impl SyncElement {
    // ── 同步方法(直接委托)────────────────────────────

    pub fn tag(&self) -> &str {
        self.inner.tag()
    }
    pub fn text(&self) -> &str {
        self.inner.text()
    }
    pub fn html(&self) -> &str {
        self.inner.html()
    }
    pub fn attr(&self, name: &str) -> Option<&str> {
        self.inner.attr(name)
    }
    pub fn attrs(&self) -> &[(String, String)] {
        self.inner.attrs()
    }
    pub fn is_displayed(&self) -> bool {
        self.inner.is_displayed()
    }
    pub fn is_enabled(&self) -> bool {
        self.inner.is_enabled()
    }
    pub fn is_cdp(&self) -> bool {
        self.inner.is_cdp()
    }

    // ── 异步方法(block_on 包装)────────────────────────

    pub fn click(&self) -> Result<()> {
        self.rt.block_on(self.inner.click())
    }
    pub fn input(&self, text: &str) -> Result<()> {
        self.rt.block_on(self.inner.input(text))
    }
    pub fn fill(&self, text: &str) -> Result<()> {
        self.rt.block_on(self.inner.fill(text))
    }
    pub fn clear(&self) -> Result<()> {
        self.rt.block_on(self.inner.clear())
    }
    pub fn hover(&self) -> Result<()> {
        self.rt.block_on(self.inner.hover())
    }
    pub fn scroll_into_view(&self) -> Result<()> {
        self.rt.block_on(self.inner.scroll_into_view())
    }
    pub fn press_key(&self, key: &str) -> Result<()> {
        self.rt.block_on(self.inner.press_key(key))
    }
    pub fn right_click(&self) -> Result<()> {
        self.rt.block_on(self.inner.right_click())
    }
    pub fn double_click(&self) -> Result<()> {
        self.rt.block_on(self.inner.double_click())
    }
    pub fn submit(&self) -> Result<()> {
        self.rt.block_on(self.inner.submit())
    }
    pub fn check(&self) -> Result<()> {
        self.rt.block_on(self.inner.check())
    }
    pub fn uncheck(&self) -> Result<()> {
        self.rt.block_on(self.inner.uncheck())
    }
    pub fn focus(&self) -> Result<()> {
        self.rt.block_on(self.inner.focus())
    }
    pub fn blur(&self) -> Result<()> {
        self.rt.block_on(self.inner.blur())
    }

    pub fn value(&self) -> Result<String> {
        self.rt.block_on(self.inner.value())
    }
    pub fn rect(&self) -> Result<(f64, f64, f64, f64)> {
        self.rt.block_on(self.inner.rect())
    }
    pub fn bounding_box(&self) -> Result<(f64, f64, f64, f64)> {
        self.rt.block_on(self.inner.bounding_box())
    }
    pub fn is_selected(&self) -> Result<bool> {
        self.rt.block_on(self.inner.is_selected())
    }
    pub fn is_visible(&self) -> bool {
        self.rt.block_on(self.inner.is_visible())
    }
    pub fn is_in_viewport(&self) -> bool {
        self.rt.block_on(self.inner.is_in_viewport())
    }
    pub fn is_alive(&self) -> bool {
        self.rt.block_on(self.inner.is_alive())
    }
    pub fn style(&self, prop: &str) -> Result<String> {
        self.rt.block_on(self.inner.style(prop))
    }
    pub fn set_value(&self, value: &str) -> Result<()> {
        self.rt.block_on(self.inner.set_value(value))
    }
    pub fn remove(&self) -> Result<()> {
        self.rt.block_on(self.inner.remove())
    }

    pub fn select(&self, text: &str) -> Result<()> {
        self.rt.block_on(self.inner.select(text))
    }
    pub fn select_by_value(&self, val: &str) -> Result<()> {
        self.rt.block_on(self.inner.select_by_value(val))
    }
    pub fn select_option(&self, val: &str) -> Result<()> {
        self.rt.block_on(self.inner.select_option(val))
    }
    pub fn select_text(&self) -> Result<()> {
        self.rt.block_on(self.inner.select_text())
    }
    pub fn upload_file(&self, path: &str) -> Result<()> {
        self.rt.block_on(self.inner.upload_file(path))
    }
    pub fn upload_files(&self, paths: &[&str]) -> Result<()> {
        self.rt.block_on(self.inner.upload_files(paths))
    }

    pub fn screenshot(&self, path: &str) -> Result<()> {
        self.rt.block_on(self.inner.screenshot(path))
    }
    pub fn screenshot_bytes(&self) -> Result<Vec<u8>> {
        self.rt.block_on(self.inner.screenshot_bytes())
    }

    pub fn drag_to(&self, target: &SyncElement) -> Result<()> {
        self.rt.block_on(self.inner.drag_to(&target.inner))
    }
    pub fn drag_to_offset(&self, x: f64, y: f64) -> Result<()> {
        self.rt.block_on(self.inner.drag_to_offset(x, y))
    }

    pub fn set_attr(&self, name: &str, val: &str) -> Result<()> {
        self.rt.block_on(self.inner.set_attr(name, val))
    }
    pub fn set_style(&self, prop: &str, val: &str) -> Result<()> {
        self.rt.block_on(self.inner.set_style(prop, val))
    }
    pub fn add_class(&self, class: &str) -> Result<()> {
        self.rt.block_on(self.inner.add_class(class))
    }
    pub fn remove_class(&self, class: &str) -> Result<()> {
        self.rt.block_on(self.inner.remove_class(class))
    }
    pub fn has_class(&self, class: &str) -> Result<bool> {
        self.rt.block_on(self.inner.has_class(class))
    }

    pub fn parent(&self) -> Result<SyncElement> {
        let el = self.rt.block_on(self.inner.parent())?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt.clone(),
        })
    }
    pub fn first_child(&self) -> Result<SyncElement> {
        let el = self.rt.block_on(self.inner.first_child())?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt.clone(),
        })
    }
    pub fn next(&self) -> Result<SyncElement> {
        let el = self.rt.block_on(self.inner.next())?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt.clone(),
        })
    }
    pub fn prev(&self) -> Result<SyncElement> {
        let el = self.rt.block_on(self.inner.prev())?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt.clone(),
        })
    }
    pub fn scroll_to_top(&self) -> Result<()> {
        self.rt.block_on(self.inner.scroll_to_top())
    }

    pub fn js(&self, script: &str) -> Result<()> {
        self.rt.block_on(self.inner.js(script))
    }

    pub fn shadow_ele(&self, selector: &str) -> Result<SyncElement> {
        let el = self.rt.block_on(self.inner.shadow_ele(selector))?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt.clone(),
        })
    }
    pub fn shadow_eles(&self, selector: &str) -> Result<Vec<SyncElement>> {
        let els = self.rt.block_on(self.inner.shadow_eles(selector))?;
        Ok(els
            .into_iter()
            .map(|e| SyncElement {
                inner: e,
                rt: self.rt.clone(),
            })
            .collect())
    }

    // ── 等待 ──────────────────────────────────────────

    pub fn wait_for_visible(&self) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_visible())
    }
    pub fn wait_for_visible_with_timeout(&self, timeout: Duration) -> Result<()> {
        self.rt
            .block_on(self.inner.wait_for_visible_with_timeout(timeout))
    }
    pub fn wait_for_hidden(&self) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_hidden())
    }
    pub fn wait_for_hidden_with_timeout(&self, timeout: Duration) -> Result<()> {
        self.rt
            .block_on(self.inner.wait_for_hidden_with_timeout(timeout))
    }
    pub fn wait_for_enabled(&self) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_enabled())
    }
    pub fn wait_for_enabled_with_timeout(&self, timeout: Duration) -> Result<()> {
        self.rt
            .block_on(self.inner.wait_for_enabled_with_timeout(timeout))
    }
    pub fn wait_for_clickable(&self) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_clickable())
    }
    pub fn wait_actionable(&self, timeout_secs: u64) -> Result<()> {
        self.rt.block_on(self.inner.wait_actionable(timeout_secs))
    }
    pub fn wait_for_stale(&self) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_stale())
    }
    pub fn wait_for_text(&self, text: &str) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_text(text))
    }
    pub fn wait_for_text_eq(&self, text: &str) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_text_eq(text))
    }
    pub fn wait_for_attribute(&self, name: &str, value: &str) -> Result<()> {
        self.rt.block_on(self.inner.wait_for_attribute(name, value))
    }
    pub fn wait_for_attribute_contains(&self, name: &str, value: &str) -> Result<()> {
        self.rt
            .block_on(self.inner.wait_for_attribute_contains(name, value))
    }

    // ── 子元素查找 ─────────────────────────────────────

    pub fn ele(&self, selector: &str) -> Result<SyncElement> {
        let el = self.inner.ele(selector)?;
        Ok(SyncElement {
            inner: el,
            rt: self.rt.clone(),
        })
    }
    pub fn eles(&self, selector: &str) -> Result<Vec<SyncElement>> {
        let els = self.inner.eles(selector)?;
        Ok(els
            .into_iter()
            .map(|e| SyncElement {
                inner: e,
                rt: self.rt.clone(),
            })
            .collect())
    }
}

/// 同步版请求拦截守卫 — 让 sync 用户拦截 / 改写 / 拒绝被暂停的请求。
///
/// 由 [`SyncPage::enable_intercept`] 返回。匹配 pattern 的请求会被 Fetch 域
/// 暂停;用 [`paused_requests`](Self::paused_requests) 取出,再
/// [`continue_request`](Self::continue_request)(可改写 URL)或
/// [`fail_request`](Self::fail_request) 放行/拒绝。`disable()` 或 drop 关闭。
pub struct SyncInterceptGuard {
    inner: InterceptGuard,
    rt: tokio::runtime::Handle,
}

impl SyncInterceptGuard {
    /// 当前被暂停(尚未 continue/fail)的请求。
    pub fn paused_requests(&self) -> Vec<InterceptedRequest> {
        self.inner.paused_requests()
    }

    /// 放行一个被暂停的请求,`new_url` 非 None 时重定向到新 URL。
    pub fn continue_request(&self, request_id: &str, new_url: Option<&str>) -> Result<()> {
        self.rt
            .block_on(self.inner.continue_request(request_id, new_url))
    }

    /// 拒绝一个被暂停的请求(按 BlockedByClient 失败)。
    pub fn fail_request(&self, request_id: &str) -> Result<()> {
        self.rt.block_on(self.inner.fail_request(request_id))
    }

    /// 用伪造响应放行被暂停的请求(对标 Playwright `route.fulfill`)。
    pub fn fulfill_request(
        &self,
        request_id: &str,
        status: u16,
        headers: &[(&str, &str)],
        body: &[u8],
    ) -> Result<()> {
        self.rt.block_on(
            self.inner
                .fulfill_request(request_id, status, headers, body),
        )
    }

    /// 便捷:用 JSON body 伪造响应(200 + application/json)。
    pub fn fulfill_json(&self, request_id: &str, json: &str) -> Result<()> {
        self.rt.block_on(self.inner.fulfill_json(request_id, json))
    }

    /// 关闭拦截(也可直接 drop 本守卫)。
    pub fn disable(&self) -> Result<()> {
        self.rt.block_on(self.inner.disable())
    }
}