playwright-cdp 0.2.0

Drive Chromium directly via the Chrome DevTools Protocol (CDP) — no Playwright driver required.
Documentation
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
//! `Frame` — a frame within a page (the main document, or an `<iframe>`).

use crate::element_handle::ElementHandle;
use crate::error::{Error, Result};
use crate::options::{
    CheckOptions, ClickOptions, DragToOptions, FillOptions, GotoOptions, HoverOptions, PressOptions,
    PressSequentiallyOptions, SelectOption, SelectOptions, WaitForFunctionOptions, WaitForOptions, WaitUntil,
};
use crate::page::Page;
use crate::response::Response;
use crate::types::AriaRole;
use serde_json::{json, Value};
use std::time::Duration;
use tokio::sync::broadcast;

/// Options for [`Frame::add_script_tag`].
///
/// Either `url` (load a remote script) or `source` (inline script text) must
/// be supplied. `type` sets the `<script type=...>` attribute (e.g.
/// `"module"` / `"text/javascript"`).
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct AddScriptTagOptions {
    /// The `src` URL of an external script to load.
    pub url: Option<String>,
    /// Inline script source text to inject as the element's text content.
    pub source: Option<String>,
    /// The `<script type=...>` attribute value (e.g. `"module"`).
    pub r#type: Option<String>,
}

impl AddScriptTagOptions {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn url(mut self, v: impl Into<String>) -> Self {
        self.url = Some(v.into());
        self
    }
    pub fn source(mut self, v: impl Into<String>) -> Self {
        self.source = Some(v.into());
        self
    }
    pub fn type_(mut self, v: impl Into<String>) -> Self {
        self.r#type = Some(v.into());
        self
    }
}

/// Options for [`Frame::add_style_tag`].
///
/// Either `url` (load a remote stylesheet) or `source` (inline CSS text) must
/// be supplied.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct AddStyleTagOptions {
    /// The `href` URL of an external stylesheet to load.
    pub url: Option<String>,
    /// Inline CSS source text to inject as the element's text content.
    pub source: Option<String>,
}

impl AddStyleTagOptions {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn url(mut self, v: impl Into<String>) -> Self {
        self.url = Some(v.into());
        self
    }
    pub fn source(mut self, v: impl Into<String>) -> Self {
        self.source = Some(v.into());
        self
    }
}

/// Options for [`Frame::wait_for_url`].
///
/// Mirrors the subset of Playwright's `FrameWaitForUrlOptions` we support: a
/// navigation-style timeout plus an optional lifecycle state to settle on.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct FrameWaitForUrlOptions {
    pub timeout: Option<Duration>,
    pub wait_until: Option<WaitUntil>,
}

impl FrameWaitForUrlOptions {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn timeout(mut self, v: Duration) -> Self {
        self.timeout = Some(v);
        self
    }
    pub fn wait_until(mut self, v: WaitUntil) -> Self {
        self.wait_until = Some(v);
        self
    }
}

/// A frame in a page. The main frame wraps the page's top-level document.
#[derive(Clone)]
pub struct Frame {
    page: Page,
    frame_id: String,
}

impl Frame {
    pub(crate) fn new(page: Page, frame_id: String) -> Self {
        Self { page, frame_id }
    }

    pub(crate) fn main(page: Page) -> Self {
        let frame_id = page.main_frame_id().unwrap_or_default();
        Self { page, frame_id }
    }

    /// The owning page.
    pub fn page(&self) -> Page {
        self.page.clone()
    }

    /// CDP frame id.
    pub fn frame_id(&self) -> &str {
        &self.frame_id
    }

    pub fn name(&self) -> String {
        self.page
            .frame_data(&self.frame_id)
            .map(|d| d.name)
            .unwrap_or_default()
    }

    pub fn url(&self) -> String {
        self.page
            .frame_data(&self.frame_id)
            .map(|d| d.url)
            .unwrap_or_default()
    }

    pub fn parent_frame(&self) -> Option<Frame> {
        let parent = self.page.frame_data(&self.frame_id)?.parent_id.clone()?;
        Some(Frame::new(self.page.clone(), parent))
    }

    pub fn child_frames(&self) -> Vec<Frame> {
        self.page
            .frame_ids_with_parent(&self.frame_id)
            .into_iter()
            .map(|id| Frame::new(self.page.clone(), id))
            .collect()
    }

    pub fn is_detached(&self) -> bool {
        self.page
            .frame_data(&self.frame_id)
            .map(|d| d.detached)
            .unwrap_or(false)
    }

    /// Evaluate `expression` in this frame's main world.
    ///
    /// Runs in the frame's own execution context (looked up from the page's
    /// per-frame context map), so for a child `<iframe>` it acts on the
    /// iframe's document/window, not the top-level page's.
    pub async fn evaluate<R: serde::de::DeserializeOwned>(&self, expression: &str) -> Result<R> {
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let function = format!("(arg) => {{ return ({expression}); }}");
        let v = crate::selectors::eval_context(self.page.session(), ctx, &function, Value::Null)
            .await?;
        serde_json::from_value::<R>(v).map_err(Error::from)
    }

    /// Evaluate `expression` in this frame's main world, returning a
    /// [`JSHandle`](crate::js_handle::JSHandle) to the resulting remote object.
    ///
    /// Mirrors [`Frame::evaluate`] but returns the object by reference rather
    /// than by value. The expression runs in the frame's own execution context
    /// (`Runtime.evaluate { contextId }`, `returnByValue: false`); the returned
    /// `objectId` is wrapped in a [`JSHandle`](crate::js_handle::JSHandle)
    /// sharing the page's session.
    pub async fn evaluate_handle(&self, expression: &str) -> Result<crate::js_handle::JSHandle> {
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let function = format!("(arg) => {{ return ({expression}); }}");
        let oid = crate::selectors::eval_context_handle(
            self.page.session(),
            ctx,
            &function,
            Value::Null,
        )
        .await?
        .ok_or_else(|| {
            Error::ProtocolError(
                "evaluate_handle did not return a remote object (no objectId)".into(),
            )
        })?;
        Ok(crate::js_handle::JSHandle::new(self.page.session_arc(), oid))
    }

    /// Navigate this frame to `url` (main frame only).
    pub async fn goto(
        &self,
        url: &str,
        opts: Option<GotoOptions>,
    ) -> Result<Option<Response>> {
        if self.frame_id != self.page.main_frame_id().unwrap_or_default() {
            return Err(Error::InvalidArgument(
                "Frame::goto on non-main frames is not supported".into(),
            ));
        }
        self.page.goto(url, opts).await
    }

    pub async fn wait_for_load_state(&self, state: Option<WaitUntil>) -> Result<()> {
        self.page.wait_for_load_state(state).await
    }

    /// Poll `expression` until truthy (delegates to the page). See
    /// [`Page::wait_for_function`].
    pub async fn wait_for_function<R: serde::de::DeserializeOwned>(
        &self,
        expression: &str,
        arg: Option<serde_json::Value>,
        options: Option<WaitForFunctionOptions>,
    ) -> Result<R> {
        self.page.wait_for_function(expression, arg, options).await
    }

    /// The serialized HTML of this frame's document (`documentElement.outerHTML`).
    pub async fn content(&self) -> Result<String> {
        self.evaluate::<String>("document.documentElement.outerHTML").await
    }

    /// Replace this frame's document content with `html`.
    ///
    /// Runs `document.open()/write()/close()` in the frame's own context, so
    /// for a child iframe it rewrites the iframe's document.
    pub async fn set_content(&self, html: &str) -> Result<()> {
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let _ = crate::selectors::eval_context(
            self.page.session(),
            ctx,
            "(html) => { document.open(); document.write(html); document.close(); }",
            json!(html),
        )
        .await?;
        Ok(())
    }

    /// The title of this frame's document (`document.title`).
    pub async fn title(&self) -> Result<String> {
        self.evaluate::<String>("document.title").await
    }

    /// Build a locator scoped to this frame's execution context.
    ///
    /// The resolved locator runs its selector queries in the frame's own
    /// context, so for a child iframe `document` is the iframe's document.
    /// This is the same-origin scoping mechanism used by [`Frame::evaluate`].
    pub fn locator(&self, selector: impl Into<String>) -> crate::locator::Locator {
        let ctx = self.page.context_for_frame(&self.frame_id);
        crate::locator::Locator::new_in_frame_ctx(
            self.page.clone(),
            ctx,
            selector.into(),
            true,
            None,
        )
    }

    pub fn get_by_text(&self, text: &str, exact: bool) -> crate::locator::Locator {
        let sel = if exact {
            format!("text=\"{text}\"")
        } else {
            format!("text={text}")
        };
        self.locator(sel)
    }

    pub fn get_by_label(&self, text: &str) -> crate::locator::Locator {
        self.locator(format!("[aria-label=\"{}\"]", attr_escape(text)))
    }

    pub fn get_by_role(
        &self,
        role: AriaRole,
        opts: Option<crate::options::GetByRoleOptions>,
    ) -> crate::locator::Locator {
        let opts = opts.unwrap_or_default();
        let mut sel = format!("role={}", role.as_str());
        if let Some(name) = &opts.name {
            sel.push_str(&format!("[name=\"{name}\"]"));
        }
        if opts.exact == Some(true) {
            sel.push_str("[exact=\"true\"]");
        }
        self.locator(sel)
    }

    pub fn get_by_placeholder(&self, text: &str) -> crate::locator::Locator {
        self.locator(format!("[placeholder=\"{}\"]", attr_escape(text)))
    }

    pub fn get_by_alt_text(&self, text: &str) -> crate::locator::Locator {
        self.locator(format!("[alt=\"{}\"]", attr_escape(text)))
    }

    pub fn get_by_title(&self, text: &str) -> crate::locator::Locator {
        self.locator(format!("[title=\"{}\"]", attr_escape(text)))
    }

    pub fn get_by_test_id(&self, text: &str) -> crate::locator::Locator {
        self.locator(format!("[data-testid=\"{}\"]", attr_escape(text)))
    }

    /// Inject a `<script>` tag into this frame.
    ///
    /// Either `url` (external script) or `source` (inline script text) must be
    /// provided; `type` sets the `<script type=...>` attribute.
    ///
    /// Implemented via DOM injection in the frame's own execution context, so
    /// `document.head` is this frame's `<head>` — for a child iframe the script
    /// is injected into (and executes within) the iframe's document.
    pub async fn add_script_tag(&self, opts: Option<AddScriptTagOptions>) -> Result<()> {
        let opts = opts.unwrap_or_default();
        if opts.url.is_none() && opts.source.is_none() {
            return Err(Error::InvalidArgument(
                "AddScriptTagOptions requires either `url` or `source`".into(),
            ));
        }
        let arg = json!({ "url": opts.url, "source": opts.source, "type": opts.r#type });
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let _ = crate::selectors::eval_context(
            self.page.session(),
            ctx,
            "(a) => { const s = document.createElement('script'); if (a.type) s.type = a.type; if (a.url) s.src = a.url; if (a.source) s.textContent = a.source; document.head.appendChild(s); }",
            arg,
        )
        .await?;
        Ok(())
    }

    /// Inject a `<link rel=stylesheet>`/`<style>` tag into this frame.
    ///
    /// Either `url` (external stylesheet) or `source` (inline CSS) must be
    /// provided. Runs in the frame's own context, so it targets this frame's
    /// `<head>`.
    pub async fn add_style_tag(&self, opts: Option<AddStyleTagOptions>) -> Result<()> {
        let opts = opts.unwrap_or_default();
        if opts.url.is_none() && opts.source.is_none() {
            return Err(Error::InvalidArgument(
                "AddStyleTagOptions requires either `url` or `source`".into(),
            ));
        }
        let arg = json!({ "url": opts.url, "source": opts.source });
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let _ = crate::selectors::eval_context(
            self.page.session(),
            ctx,
            "(a) => { if (a.url) { const l = document.createElement('link'); l.rel = 'stylesheet'; l.href = a.url; document.head.appendChild(l); } else { const s = document.createElement('style'); s.textContent = a.source; document.head.appendChild(s); } }",
            arg,
        )
        .await?;
        Ok(())
    }

    /// Wait until this frame's URL matches `url` (exact, or `*` glob).
    ///
    /// Subscribes to the page session and watches `Page.frameNavigated` /
    /// `Page.lifecycleEvent` events for this frame, falling back to the cached
    /// frame tree URL. On match, optionally waits for a lifecycle state.
    pub async fn wait_for_url(&self, url: &str, opts: Option<FrameWaitForUrlOptions>) -> Result<()> {
        let opts = opts.unwrap_or_default();
        let timeout = opts
            .timeout
            .unwrap_or_else(|| self.page.default_navigation_timeout());
        let deadline = tokio::time::Instant::now() + timeout;

        // Fast path: already matches.
        if glob_matches(url, &self.url()) {
            if let Some(state) = opts.wait_until {
                self.wait_for_load_state(Some(state)).await?;
            }
            return Ok(());
        }

        // Subscribe before polling so navigation/lifecycle events for this
        // frame drive the loop (the background frame tracker refreshes the
        // cached URL from `Page.frameNavigated`).
        let mut rx = self.page.session().subscribe();
        loop {
            // Re-check the cached URL (kept fresh by the frame tracker task).
            if glob_matches(url, &self.url()) {
                break;
            }
            if tokio::time::Instant::now() >= deadline {
                return Err(Error::Timeout(format!(
                    "wait_for_url '{url}' timed out after {}ms (last: '{}')",
                    timeout.as_millis(),
                    self.url()
                )));
            }
            // Block until the next CDP event, bounded by the remaining budget.
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            match tokio::time::timeout(remaining, rx.recv()).await {
                Ok(Ok(ev))
                    if frame_event_matches(&ev, &self.frame_id) =>
                {
                    continue;
                }
                Ok(Ok(_)) => continue,
                Ok(Err(broadcast::error::RecvError::Closed)) => {
                    return Err(Error::ChannelClosed);
                }
                Ok(Err(broadcast::error::RecvError::Lagged(_))) => continue,
                Err(_) => {
                    return Err(Error::Timeout(format!(
                        "wait_for_url '{url}' timed out after {}ms (last: '{}')",
                        timeout.as_millis(),
                        self.url()
                    )));
                }
            }
        }
        if let Some(state) = opts.wait_until {
            self.wait_for_load_state(Some(state)).await?;
        }
        Ok(())
    }

    // --- Frame action methods (thin delegations to locators) ---
    //
    // Each resolves a locator for `selector` and forwards the call, mirroring
    // Playwright's `frame.*` API shape (selector first, options last).

    /// Click the first element matching `selector`.
    pub async fn click(&self, selector: &str, opts: Option<ClickOptions>) -> Result<()> {
        self.locator(selector).click(opts).await
    }

    /// Double-click the first element matching `selector`.
    pub async fn dblclick(&self, selector: &str, opts: Option<ClickOptions>) -> Result<()> {
        self.locator(selector).dblclick(opts).await
    }

    /// Set the value of the first matching `<input>`/`<textarea>` to `value`.
    pub async fn fill(&self, selector: &str, value: &str, opts: Option<FillOptions>) -> Result<()> {
        self.locator(selector).fill(value, opts).await
    }

    /// Focus the first element matching `selector`.
    pub async fn focus(&self, selector: &str) -> Result<()> {
        self.locator(selector).focus().await
    }

    /// Hover the first element matching `selector`.
    pub async fn hover(&self, selector: &str, opts: Option<HoverOptions>) -> Result<()> {
        self.locator(selector).hover(opts).await
    }

    /// Press `key` on the first element matching `selector`.
    pub async fn press(&self, selector: &str, key: &str, opts: Option<PressOptions>) -> Result<()> {
        self.locator(selector).press(key, opts).await
    }

    /// Select `<option>`(s) on the first matching `<select>` by value/label/index.
    pub async fn select_option(
        &self,
        selector: &str,
        value: impl Into<SelectOption>,
        opts: Option<SelectOptions>,
    ) -> Result<Vec<String>> {
        self.locator(selector).select_option(value, opts).await
    }

    /// Set files on the first matching `<input type=file>` by server-side path(s).
    pub async fn set_input_files(&self, selector: &str, files: &[&str]) -> Result<()> {
        self.locator(selector).set_input_files(files).await
    }

    /// Tap (touch) the first element matching `selector`.
    pub async fn tap(&self, selector: &str, opts: Option<ClickOptions>) -> Result<()> {
        self.locator(selector).tap(opts).await
    }

    /// Check the first matching checkbox/radio.
    pub async fn check(&self, selector: &str, opts: Option<CheckOptions>) -> Result<()> {
        self.locator(selector).check(opts).await
    }

    /// Uncheck the first matching checkbox.
    pub async fn uncheck(&self, selector: &str, opts: Option<CheckOptions>) -> Result<()> {
        self.locator(selector).uncheck(opts).await
    }

    /// Set the checked state of the first matching checkbox/radio.
    pub async fn set_checked(
        &self,
        selector: &str,
        checked: bool,
        opts: Option<CheckOptions>,
    ) -> Result<()> {
        self.locator(selector)
            .set_checked(checked, opts)
            .await
    }

    /// Type `text` into the first element matching `selector` (fill-based).
    pub async fn type_(
        &self,
        selector: &str,
        text: &str,
        opts: Option<PressSequentiallyOptions>,
    ) -> Result<()> {
        self.locator(selector).type_(text, opts).await
    }

    /// The `textContent` of the first element matching `selector`, if any.
    pub async fn text_content(&self, selector: &str) -> Result<Option<String>> {
        self.locator(selector).text_content().await
    }

    /// The visible text of the first element matching `selector`.
    pub async fn inner_text(&self, selector: &str) -> Result<String> {
        self.locator(selector).inner_text().await
    }

    /// The serialized HTML of the first element matching `selector`.
    pub async fn inner_html(&self, selector: &str) -> Result<String> {
        self.locator(selector).inner_html().await
    }

    /// The value of attribute `name` on the first element matching `selector`.
    pub async fn get_attribute(&self, selector: &str, name: &str) -> Result<Option<String>> {
        self.locator(selector).get_attribute(name).await
    }

    /// Number of elements matching `selector` in this frame.
    pub async fn count(&self, selector: &str) -> Result<usize> {
        self.locator(selector).count().await
    }

    /// Wait until an element matching `selector` resolves in this frame.
    pub async fn wait_for_selector(
        &self,
        selector: &str,
        opts: Option<WaitForOptions>,
    ) -> Result<()> {
        self.locator(selector).wait_for(opts).await
    }

    /// Evaluate `expression` against the first element matching `selector` in
    /// this frame.
    pub async fn eval_on_selector<R: serde::de::DeserializeOwned>(
        &self,
        selector: &str,
        expression: &str,
    ) -> Result<R> {
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let object_id = crate::selectors::element_at(self.page.session(), ctx, selector, 0)
            .await?
            .ok_or_else(|| Error::ElementNotFound(selector.to_string()))?;
        let function = format!("(el) => {{ return ({expression}); }}");
        let v = crate::selectors::eval_object(self.page.session(), &object_id, &function, Value::Null)
            .await?;
        self.release_object(&object_id).await;
        serde_json::from_value::<R>(v).map_err(Error::from)
    }

    /// Evaluate `expression` against all elements matching `selector` in this
    /// frame.
    pub async fn eval_on_selector_all<R: serde::de::DeserializeOwned>(
        &self,
        selector: &str,
        expression: &str,
    ) -> Result<Vec<R>> {
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let n = crate::selectors::count(self.page.session(), ctx, selector).await?;
        let mut out = Vec::with_capacity(n);
        for i in 0..n {
            if let Some(oid) =
                crate::selectors::element_at(self.page.session(), ctx, selector, i).await?
            {
                let function = format!("(el) => {{ return ({expression}); }}");
                let v =
                    crate::selectors::eval_object(self.page.session(), &oid, &function, Value::Null)
                        .await?;
                self.release_object(&oid).await;
                out.push(serde_json::from_value::<R>(v).map_err(Error::from)?);
            }
        }
        Ok(out)
    }

    /// Dispatch a synthetic DOM event on the first element matching `selector`.
    pub async fn dispatch_event(
        &self,
        selector: &str,
        type_: &str,
        init: Option<Value>,
    ) -> Result<()> {
        self.locator(selector)
            .dispatch_event(type_, init)
            .await
    }

    /// Drag the element matching `source` onto the element matching `target`.
    pub async fn drag_and_drop(
        &self,
        source: &str,
        target: &str,
        opts: Option<DragToOptions>,
    ) -> Result<()> {
        self.locator(source).drag_to(&self.locator(target), opts).await
    }

    /// The first element matching `selector` in this frame, if any.
    pub async fn query_selector(&self, selector: &str) -> Result<Option<ElementHandle>> {
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let oid = crate::selectors::element_at(self.page.session(), ctx, selector, 0)
            .await?
            .map(|oid| ElementHandle::new(self.page.clone(), oid));
        Ok(oid)
    }

    /// All elements matching `selector` in this frame.
    pub async fn query_selector_all(&self, selector: &str) -> Result<Vec<ElementHandle>> {
        let ctx = self.page.ctx_for_frame(&self.frame_id).await;
        let n = crate::selectors::count(self.page.session(), ctx, selector).await?;
        let mut out = Vec::with_capacity(n);
        for i in 0..n {
            if let Some(oid) =
                crate::selectors::element_at(self.page.session(), ctx, selector, i).await?
            {
                out.push(ElementHandle::new(self.page.clone(), oid));
            }
        }
        Ok(out)
    }

    async fn release_object(&self, object_id: &str) {
        let _ = self
            .page
            .session()
            .send("Runtime.releaseObject", json!({ "objectId": object_id }))
            .await;
    }

    /// Keep the `Value` import referenced (used by callers wiring args).
    #[allow(dead_code)]
    fn _json_marker(&self) -> Value {
        serde_json::json!({})
    }
}

/// Escape a string for safe embedding in a CSS-attribute selector.
fn attr_escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Whether a CDP event is one that can change this frame's URL/state — i.e. a
/// `Page.frameNavigated` or `Page.lifecycleEvent` whose `frameId`/`frame.id`
/// matches `frame_id`.
fn frame_event_matches(ev: &crate::cdp::CdpEvent, frame_id: &str) -> bool {
    match ev.method.as_str() {
        "Page.frameNavigated" => ev
            .params
            .get("frame")
            .and_then(|f| f.get("id"))
            .and_then(|v| v.as_str())
            == Some(frame_id),
        "Page.lifecycleEvent" => {
            ev.params.get("frameId").and_then(|v| v.as_str()) == Some(frame_id)
        }
        _ => false,
    }
}

/// Match `pattern` against `value`: exact, or a leading/trailing/both `*` glob.
///
/// Mirrors the helper in `src/page.rs` used by `Page::wait_for_url`.
fn glob_matches(pattern: &str, value: &str) -> bool {
    if pattern == value {
        return true;
    }
    // `*middle*` → contains.
    if pattern.len() >= 2 && pattern.starts_with('*') && pattern.ends_with('*') {
        return value.contains(&pattern[1..pattern.len() - 1]);
    }
    if let Some(rest) = pattern.strip_prefix('*') {
        if value.ends_with(rest) {
            return true;
        }
    }
    if let Some(rest) = pattern.strip_suffix('*') {
        if value.starts_with(rest) {
            return true;
        }
    }
    false
}