ferridriver 0.4.0

Browser automation in Rust with a Playwright-compatible API. Four pluggable backends: CDP pipe, CDP WebSocket, Playwright WebKit, Firefox BiDi.
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
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
//! Lazy element locator.
//!
//! A Locator stores a selector string and a reference to its Page.
//! It does NOT query the DOM when created. Resolution happens lazily
//! when an action method (click, fill, etc.) is called.
//!
//! Locators can be chained to narrow scope:
//! ```ignore
//! page.locator("css=.form", None).get_by_role("textbox", &Default::default()).fill("hello", None).await?;
//! ```

use std::fmt::Write as _;
use std::sync::Arc;

use crate::actions;
use crate::backend::AnyElement;
use crate::error::Result;
use crate::options::{BoundingBox, FilterOptions, RoleOptions, StringOrRegex, TextOptions, WaitOptions};
use crate::selectors;

/// Zero-cost retry macro that resolves an element with backoff, then runs an
/// action body inline. Provides `$el: AnyElement` and `$page: &AnyPage` to the
/// body without any `AnyPage` cloning — the page reference is borrowed from `self`
/// for the entire retry loop.
///
/// The body must be an `async move { ... }` block returning
/// [`crate::error::Result<R>`]. The macro forwards every error through
/// the [`crate::error::FerriError`] taxonomy so call sites declare
/// `-> crate::error::Result<R>`.
///
/// `$timeout_ms` is an `Option<u64>` — the per-call override from the action's
/// option bag. `None` falls back to `page.default_timeout()` (set via
/// `page.setDefaultTimeout`). A resolved value of `0` means "no timeout" and
/// loops forever. `$op` is a `&str` used in the timeout-error message
/// (`TimeoutError { while $op }`).
///
/// Polling schedule: `[0, 0, 20, 50, 100, 100, 500]`, clamped at the last
/// value on overflow.
macro_rules! retry_resolve {
  ($self:expr, $timeout_ms:expr, $op:expr, |$el:ident, $page:ident| $body:expr) => {{
    // Resolve `frameLocator` enter-frame hops to the real child frame
    // + trailing selector (no-op for plain selectors).
    let (__rframe, __rsel) = $self.resolved().await.map_err($crate::error::FerriError::from)?;
    let $page: &$crate::backend::AnyPage = __rframe.page_arc().inner();
    $page
      .ensure_engine_injected()
      .await
      .map_err($crate::error::FerriError::from)?;
    let __fd = "window.__fd";
    let __sel_js =
      $crate::selectors::build_selone_js(&__rsel, &__fd, $self.strict).map_err($crate::error::FerriError::from)?;
    // Pass `None` for main-frame locators so the backend skips a
    // `frame_contexts` lookup; child frames thread their cached id.
    let __frame_id: ::std::option::Option<&str> = if __rframe.is_main_frame() {
      ::std::option::Option::None
    } else {
      ::std::option::Option::Some(__rframe.id())
    };

    let __op_name: &str = $op;
    let __resolved_timeout: u64 = $timeout_ms.unwrap_or_else(|| $self.frame.page_arc().default_timeout());
    let __deadline: ::std::option::Option<::std::time::Instant> = if __resolved_timeout == 0 {
      ::std::option::Option::None
    } else {
      ::std::option::Option::Some(::std::time::Instant::now() + ::std::time::Duration::from_millis(__resolved_timeout))
    };

    let mut __idx: usize = 0;
    loop {
      // Deadline check up-front so we never race into one more attempt after
      // time has already run out.
      if let ::std::option::Option::Some(__d) = __deadline {
        if ::std::time::Instant::now() >= __d {
          return ::std::result::Result::Err($crate::error::FerriError::timeout(
            __op_name.to_string(),
            __resolved_timeout,
          ));
        }
      }

      // Action pre-checks: run registered locator handlers if any of their
      // overlays are currently visible (Playwright `performActionPreChecks`).
      $crate::locator_handler::perform_checkpoint(__rframe.page_arc()).await;

      let __delay_ms = Locator::RETRY_BACKOFFS_MS[__idx.min(Locator::RETRY_BACKOFFS_MS.len() - 1)];
      __idx = __idx.saturating_add(1);
      if __delay_ms > 0 {
        // Clamp the sleep to whatever's left on the deadline so the timeout
        // error fires on time rather than after an overshoot sleep.
        let __sleep_ms = match __deadline {
          ::std::option::Option::Some(__d) => {
            let __left = u64::try_from(__d.saturating_duration_since(::std::time::Instant::now()).as_millis())
              .unwrap_or(__delay_ms);
            __delay_ms.min(__left)
          },
          ::std::option::Option::None => __delay_ms,
        };
        if __sleep_ms > 0 {
          ::tokio::time::sleep(::std::time::Duration::from_millis(__sleep_ms)).await;
        }
      }

      // Strict mode (the default) is folded into the same engine-side
      // `selOne(parts, strict)` call below — the JS throws
      // `strict mode violation: <count>` when the selector matches more
      // than one element, the host catches the exception and converts
      // to a typed `FerriError::StrictModeViolation`. Saves the
      // separate `query_all` + `cleanup_tags` round-trips the previous
      // implementation paid on every retry attempt (~2 RTTs).
      match $crate::selectors::query_one_prebuilt($page, &__sel_js, &$self.selector, __frame_id).await {
        ::std::result::Result::Ok($el) => match ($body).await {
          ::std::result::Result::Ok(val) => return ::std::result::Result::Ok(val),
          ::std::result::Result::Err(e) => {
            let __msg = e.to_string();
            if __msg.contains("not connected")
              || __msg.contains("not found")
              || __msg.contains("detached")
              || __msg.contains("error:not")
            {
              // Retriable: `checkElementStates` returns
              // `error:notvisible` / `error:notenabled` /
              // `error:noteditable` etc. as signals to keep polling until
              // the deadline.
            } else {
              return ::std::result::Result::Err($crate::error::FerriError::from(e));
            }
          },
        },
        ::std::result::Result::Err(__err) => {
          // Strict-mode violation: the engine threw
          // `strict mode violation: <count>` from inside `selOne`.
          // Surface it as a typed error rather than a retry signal.
          if let ::std::option::Option::Some(__count) = $crate::selectors::parse_strict_violation_count(&__err) {
            return ::std::result::Result::Err($crate::error::FerriError::strict($self.selector.clone(), __count));
          }
          // Otherwise: element not found this iteration; retry until deadline.
        },
      }
    }
  }};
}

/// A lazy element locator bound to a [`crate::Frame`]. Every Locator
/// carries a Frame reference, and all DOM resolution and action dispatch
/// happens in that frame's execution context. Chaining (`.locator()`,
/// `.filter()`, `.first()`, etc.) returns a new Locator on the same
/// Frame; the Frame itself is cheap to clone (two `Arc`s).
#[derive(Clone)]
pub struct Locator {
  /// Owning frame. Provides the page back-reference (`frame.page_arc()`)
  /// and the execution-context id (`frame.id()`) used by every action.
  pub(crate) frame: crate::frame::Frame,
  pub(crate) selector: String,
  /// Strict mode: error with [`crate::error::FerriError::StrictModeViolation`]
  /// if the selector resolves to multiple elements. Every Locator action
  /// runs in strict mode by default; `first()` / `last()` / `nth()` /
  /// `strict(false)` opt out.
  pub(crate) strict: bool,
}

impl Locator {
  /// Construct a Locator with strict mode enabled (the default).
  #[must_use]
  pub(crate) fn new(frame: crate::frame::Frame, selector: String) -> Self {
    Self {
      frame,
      selector,
      strict: true,
    }
  }

  /// Resolve any `internal:control=enter-frame` hops embedded in
  /// `self.selector` into the actual child [`Frame`], returning the
  /// deepest frame plus the trailing selector to run inside it. A
  /// no-op clone for selectors without a frame hop.
  ///
  /// `FrameLocator` builds a selector chain like
  /// `#if >> internal:control=enter-frame >> #btn`. The injected
  /// engine's `enter-frame` control returns `[]` by design — the
  /// boundary is resolved HERE (server side), mirroring Playwright's
  /// frame chunking: query the `<iframe>` in the current frame, hop to
  /// its content frame, continue with the next chunk. Re-resolved on
  /// every action attempt so a re-attached iframe is picked up.
  pub(crate) async fn resolved(&self) -> Result<(crate::frame::Frame, String)> {
    const MARK: &str = ">> internal:control=enter-frame >>";
    if !self.selector.contains("internal:control=enter-frame") {
      return Ok((self.frame.clone(), self.selector.clone()));
    }
    let mut parts = self.selector.split(MARK).map(str::trim);
    let mut cur = self.frame.clone();
    let mut pending = parts.next().unwrap_or("").to_string();
    for next in parts {
      let page_arc = std::sync::Arc::clone(cur.page_arc());
      let fid: Option<String> = if cur.is_main_frame() {
        None
      } else {
        Some(cur.id().to_string())
      };
      let el = crate::selectors::query_one(page_arc.inner(), &pending, false, fid.as_deref()).await?;
      let handle = crate::element_handle::ElementHandle::from_any_element(std::sync::Arc::clone(&page_arc), el).await?;
      cur = handle
        .content_frame()
        .await?
        .ok_or_else(|| crate::error::FerriError::protocol("frameLocator", "<iframe> has no content frame"))?;
      pending = next.to_string();
    }
    Ok((cur, pending))
  }

  /// Returns a copy of this locator with strict-mode toggled.
  ///
  /// In strict mode (default), any action on a locator that matches more than
  /// one element raises [`crate::error::FerriError::StrictModeViolation`].
  /// Pass `false` to explicitly allow multi-match (the behaviour of
  /// `locator.first()` / `.last()` / `.nth()`).
  #[must_use]
  pub fn strict(&self, strict: bool) -> Locator {
    Locator {
      frame: self.frame.clone(),
      selector: self.selector.clone(),
      strict,
    }
  }
  // ── Sub-locators (chain with >>) ──────────────────────────────────────────

  /// Narrow this locator's scope.
  ///
  /// `locator(selectorOrLocator: string | Locator,
  ///          options?: Omit<LocatorOptions, 'visible'>): Locator`.
  ///
  /// Infallible by design — chainable Locator API. A cross-page inner
  /// locator encodes a sentinel clause that the selector engine rejects
  /// at resolve time; JSON encoding never fails for a valid UTF-8
  /// selector. `visible` is stripped from the option bag (only
  /// `filter()` and the constructor accept it).
  #[must_use]
  pub fn locator(
    &self,
    selector_or_locator: impl Into<crate::options::LocatorLike>,
    options: Option<crate::options::FilterOptions>,
  ) -> Locator {
    let inner = selector_or_locator.into();
    let base = match &inner {
      crate::options::LocatorLike::Selector(s) => self.chain(s),
      crate::options::LocatorLike::Locator(l) => {
        if Arc::ptr_eq(self.frame.page_arc(), l.frame.page_arc()) {
          self.chain(&format!("internal:chain={}", json_quote(&l.selector)))
        } else {
          // Encoded sentinel — the selector engine rejects it, so the
          // caller sees an explicit InvalidSelector at the first action
          // rather than a silently-wrong filter. Deferred to resolve
          // time to keep the Locator chain API infallible.
          self.chain("internal:cross-frame-error=true")
        }
      },
    };
    match options {
      Some(mut opts) => {
        opts.visible = None; // Omit<LocatorOptions, 'visible'>
        base.filter(&opts)
      },
      None => base,
    }
  }

  /// Locate elements by ARIA role, optionally filtered by role options.
  /// `options.name` accepts `string | RegExp` — passing a regex matches
  /// the accessible name with its full JS regex semantics (flags
  /// preserved), while a literal string matches case-insensitively
  /// unless `exact: true`.
  #[must_use]
  pub fn get_by_role(&self, role: &str, opts: &RoleOptions) -> Locator {
    self.chain(&build_role_selector(role, opts))
  }

  /// Locate elements by visible text content. `text` accepts
  /// `string | RegExp` per Playwright's `getByText`.
  #[must_use]
  pub fn get_by_text(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.chain(&build_text_like_selector("internal:text", text, opts))
  }

  /// Locate form elements by their associated label text. Accepts
  /// `string | RegExp` — the `getByLabel` form.
  #[must_use]
  pub fn get_by_label(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.chain(&build_text_like_selector("internal:label", text, opts))
  }

  /// Locate input elements by their placeholder text. Accepts
  /// `string | RegExp` — the `getByPlaceholder` form.
  #[must_use]
  pub fn get_by_placeholder(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.chain(&build_attr_selector("placeholder", text, opts))
  }

  /// Locate elements by their `alt` attribute text. Accepts
  /// `string | RegExp` — the `getByAltText` form.
  #[must_use]
  pub fn get_by_alt_text(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.chain(&build_attr_selector("alt", text, opts))
  }

  /// Locate elements by their `title` attribute text. Accepts
  /// `string | RegExp` — the `getByTitle` form.
  #[must_use]
  pub fn get_by_title(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.chain(&build_attr_selector("title", text, opts))
  }

  /// Locate elements by their `data-testid` (or the configured
  /// test-id attribute). Accepts `string | RegExp` — the `getByTestId`
  /// form. Matches are always exact.
  #[must_use]
  pub fn get_by_test_id(&self, test_id: &StringOrRegex) -> Locator {
    self.chain(&build_testid_selector("data-testid", test_id))
  }

  /// First element. Opts out of strict mode because the selector explicitly
  /// narrows to a single match.
  #[must_use]
  pub fn first(&self) -> Locator {
    self.chain("nth=0").strict(false)
  }

  /// Last element. Opts out of strict mode (explicit single match).
  #[must_use]
  pub fn last(&self) -> Locator {
    self.chain("nth=-1").strict(false)
  }

  /// nth element. Opts out of strict mode (explicit single match).
  #[must_use]
  pub fn nth(&self, index: i32) -> Locator {
    self.chain(&format!("nth={index}")).strict(false)
  }

  /// Filter this locator by text content, inner-locator presence/absence,
  /// or visibility. Option-to-selector encoding:
  ///
  /// * `has_text` → ` >> internal:has-text=<escaped>` (plain-text clause).
  /// * `has_not_text` → ` >> internal:has-not-text=<escaped>`.
  /// * `has` (inner [`Locator`]) → ` >> internal:has=<JSON inner selector>`.
  /// * `has_not` (inner [`Locator`]) → ` >> internal:has-not=<JSON inner selector>`.
  /// * `visible: Some(b)` → ` >> visible=true|false`.
  ///
  /// Inner locators must belong to the same page as `self`; otherwise
  /// this returns a locator whose selector contains an explicit error
  /// marker — when resolved, the selector engine rejects it and the
  /// caller sees an [`crate::error::FerriError::InvalidSelector`]. The
  /// method itself stays infallible.
  #[must_use]
  pub fn filter(&self, opts: &FilterOptions) -> Locator {
    use std::fmt::Write as _;

    // Build the combined filter suffix in one buffer, then chain once.
    let mut suffix = String::new();
    let push_sep = |buf: &mut String| {
      if !buf.is_empty() {
        buf.push_str(" >> ");
      }
    };

    if let Some(text) = &opts.has_text {
      let _ = write!(suffix, "internal:has-text={}", json_quote(text));
    }
    if let Some(text) = &opts.has_not_text {
      push_sep(&mut suffix);
      let _ = write!(suffix, "internal:has-not-text={}", json_quote(text));
    }
    if let Some(inner) = &opts.has {
      push_sep(&mut suffix);
      if inner
        .as_locator()
        .is_some_and(|l| !Arc::ptr_eq(self.frame.page_arc(), l.frame.page_arc()))
      {
        // Same-page invariant violation — inject a sentinel the selector
        // engine will reject so the caller sees an explicit error rather
        // than a silently-mismatched filter. Only enforceable when the
        // caller supplied a full `Locator`; raw selector strings skip
        // this check by design.
        let _ = write!(suffix, "internal:has-cross-frame-error=true");
      } else {
        let _ = write!(suffix, "internal:has={}", json_quote(inner.as_selector()));
      }
    }
    if let Some(inner) = &opts.has_not {
      push_sep(&mut suffix);
      if inner
        .as_locator()
        .is_some_and(|l| !Arc::ptr_eq(self.frame.page_arc(), l.frame.page_arc()))
      {
        let _ = write!(suffix, "internal:has-not-cross-frame-error=true");
      } else {
        let _ = write!(suffix, "internal:has-not={}", json_quote(inner.as_selector()));
      }
    }
    if let Some(v) = opts.visible {
      push_sep(&mut suffix);
      let _ = write!(suffix, "visible={}", if v { "true" } else { "false" });
    }
    if suffix.is_empty() {
      self.clone()
    } else {
      self.chain(&suffix)
    }
  }

  // ── Actions ───────────────────────────────────────────────────────────────
  //
  // All action methods use the `retry_resolve!` macro which:
  //   1. Pre-builds selector JS once (no re-parsing per retry)
  //   2. Borrows `&AnyPage` from self — zero AnyPage clones
  //   3. Borrows `&str` parameters directly — zero String clones
  //   4. Expands inline — no closure/future type-erasure overhead

  /// Click the element matched by this locator with the full
  /// [`crate::options::ClickOptions`] surface.
  ///
  /// All options (`button`, `click_count`, `delay`, `force`, `modifiers`,
  /// `position`, `steps`, `trial`, `timeout`) are honored across all
  /// four backends; `no_wait_after` is accepted for signature parity
  /// but has no effect (ferridriver does not implicitly wait for
  /// navigation after click).
  ///
  /// Pass `None` for the common no-options path.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found, is not actionable
  /// (unless `force=true`), or the click dispatch fails.
  pub async fn click(&self, opts: Option<crate::options::ClickOptions>) -> Result<()> {
    let opts = opts.unwrap_or_default();
    // Borrow `opts` across retry iterations — references are `Copy`, so
    // each `async move` closure captures a fresh ref instead of moving
    // the owned `ClickOptions` (which contains a non-Copy `Vec<Modifier>`).
    let opts_ref = &opts;
    retry_resolve!(self, opts_ref.timeout, "click", |el, page| async move {
      // Playwright `waitForSignalsCreatedBy`: snapshot nav state, click, then
      // if the click started a navigation wait (bounded, best-effort) for it
      // to commit so a following read/action sees the new document. Zero cost
      // when the click navigates nowhere.
      let snap = page.nav_snapshot();
      actions::click_with_opts(&el, page, opts_ref).await?;
      if !opts_ref.is_trial() {
        page.settle_navigation(snap, 2_000).await;
      }
      Ok::<(), crate::error::FerriError>(())
    })
  }

  /// Double-click the element matched by this locator.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or the double-click fails.
  pub async fn dblclick(&self, opts: Option<crate::options::DblClickOptions>) -> Result<()> {
    // `dblclick` is a click pair with `clickCount` = 1 then 2. The
    // shared `click_with_opts` honors that when `click_count` is set
    // to `2`.
    let click_opts = opts.unwrap_or_default().into_click_options();
    let click_opts_ref = &click_opts;
    retry_resolve!(self, click_opts_ref.timeout, "dblclick", |el, page| async move {
      let snap = page.nav_snapshot();
      actions::click_with_opts(&el, page, click_opts_ref).await?;
      if !click_opts_ref.is_trial() {
        page.settle_navigation(snap, 2_000).await;
      }
      Ok::<(), crate::error::FerriError>(())
    })
  }

  /// Right-click (context menu click) on the element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found, its bounding box
  /// cannot be computed, or the right-click dispatch fails.
  pub async fn right_click(&self) -> Result<()> {
    retry_resolve!(
      self,
      ::std::option::Option::<u64>::None,
      "right_click",
      |el, page| async move {
        let center = el.call_js_fn_value(
        "function() { this.scrollIntoViewIfNeeded ? this.scrollIntoViewIfNeeded() : this.scrollIntoView({block: 'center', inline: 'center'}); var r = this.getBoundingClientRect(); return {x: r.x + r.width/2, y: r.y + r.height/2}; }"
      ).await?;
        if let Some(c) = center {
          let x = c.get("x").and_then(serde_json::Value::as_f64).unwrap_or(0.0);
          let y = c.get("y").and_then(serde_json::Value::as_f64).unwrap_or(0.0);
          page.click_at_opts(x, y, "right", 1).await?;
        }
        Ok::<(), crate::error::FerriError>(())
      }
    )
  }

  /// Fill an input or textarea element with the given value.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or is not a fillable element.
  pub async fn fill(&self, value: &str, opts: Option<crate::options::FillOptions>) -> Result<()> {
    let opts = opts.unwrap_or_default();
    let force = opts.is_force();
    let opts_ref = &opts;
    retry_resolve!(self, opts_ref.timeout, "fill", |el, page| async move {
      // `actions::fill(..., force)` runs `checkElementStates(['visible',
      // 'enabled','editable'])` internally when `force` is false and
      // returns the `error:not<state>` marker the retry loop knows to
      // keep polling on. `force=true` jumps straight to the DOM write.
      actions::fill(&el, page, value, force).await
    })
  }

  /// Clear the value of an input or textarea element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found.
  pub async fn clear(&self) -> Result<()> {
    retry_resolve!(
      self,
      ::std::option::Option::<u64>::None,
      "clear",
      |el, _page| async move {
        el.call_js_fn(
          "function() { \
        if (window.__fd) window.__fd.clearAndDispatch(this); \
        else { this.value = ''; } \
      }",
        )
        .await?;
        Ok::<(), crate::error::FerriError>(())
      }
    )
  }

  /// Show the element-highlight overlay for this locator's selector.
  ///
  /// Playwright:
  /// `highlight(options: { style?: string | Record<string, string | number> }): Promise<Disposable>`
  /// (`/tmp/playwright/packages/playwright-core/src/client/locator.ts:158`).
  /// The optional `style` is collapsed to a CSS declaration string (see
  /// [`crate::options::HighlightStyle::to_css_string`]) and applied to the
  /// highlight box. The overlay re-resolves the selector on each animation
  /// frame, so no element wait happens here — matching Playwright, which
  /// just forwards to `frame._highlight`. Returns a
  /// [`crate::disposable::Disposable`] whose `dispose()` hides the overlay
  /// (Playwright returns a `DisposableStub` wrapping `hideHighlight`).
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing, frame resolution, or the
  /// injected `addHighlight` call fails.
  pub async fn highlight(
    &self,
    style: Option<crate::options::HighlightStyle>,
  ) -> Result<crate::disposable::Disposable> {
    let (frame, selector) = self.resolved().await?;
    let css = style.as_ref().map(crate::options::HighlightStyle::to_css_string);
    frame.highlight(&selector, css.as_deref()).await?;
    let this = self.clone();
    Ok(crate::disposable::Disposable::new(move || async move {
      match this.hide_highlight().await {
        Ok(()) => Ok(()),
        Err(e) if e.is_target_closed_error() => Ok(()),
        Err(e) => Err(e),
      }
    }))
  }

  /// Hide the element-highlight overlay shown by [`Locator::highlight`].
  ///
  /// Playwright: `hideHighlight(): Promise<void>`
  /// (`/tmp/playwright/packages/playwright-core/src/client/locator.ts:164`).
  /// Tears down the whole overlay for this locator's frame.
  ///
  /// # Errors
  ///
  /// Returns an error if frame resolution or the injected `hideHighlight`
  /// call fails.
  pub async fn hide_highlight(&self) -> Result<()> {
    let (frame, _selector) = self.resolved().await?;
    frame.hide_highlight().await
  }

  /// Type text into the element character by character using keyboard events.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or key dispatch fails.
  pub async fn r#type(&self, text: &str, opts: Option<crate::options::TypeOptions>) -> Result<()> {
    let opts = opts.unwrap_or_default();
    let delay_ms = opts.resolved_delay_ms();
    let timeout_ms = opts.timeout;
    retry_resolve!(self, timeout_ms, "type", |el, page| async move {
      actions::wait_for_actionable(&el, page).await.ok();
      if delay_ms > 0 {
        // With a per-char delay, fall back to the character-by-character
        // keyboard dispatch (same code path `pressSequentially` uses).
        for ch in text.chars() {
          page.press_key(&ch.to_string()).await?;
          tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
        }
        Ok(())
      } else {
        el.type_str(text).await
      }
    })
  }

  /// Press a key or key combination (e.g. "Enter", "Control+a").
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or the key press fails.
  pub async fn press(&self, key: &str, opts: Option<crate::options::PressOptions>) -> Result<()> {
    let opts = opts.unwrap_or_default();
    let delay_ms = opts.resolved_delay_ms();
    let timeout_ms = opts.timeout;
    retry_resolve!(self, timeout_ms, "press", |el, page| async move {
      actions::wait_for_actionable(&el, page).await.ok();
      // Focus the element before dispatching keys so the event lands at
      // the intended target (`_press` → `_focus` → `keyboard.press`).
      // Without this the key dispatches to whatever's currently focused,
      // usually the body, and the element under the locator never sees
      // it.
      el.call_js_fn("function() { this.focus(); }").await?;
      if delay_ms > 0 {
        // With a delay, press is equivalent to keyDown + sleep(delay)
        // + keyUp so the page observes the held-key interval.
        page.key_down(key).await?;
        tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
        page.key_up(key).await
      } else {
        page.press_key(key).await
      }
    })
  }

  /// Hover over the element matched by this locator.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or the hover action fails.
  pub async fn hover(&self, opts: Option<crate::options::HoverOptions>) -> Result<()> {
    let opts = opts.unwrap_or_default();
    let opts_ref = &opts;
    retry_resolve!(self, opts_ref.timeout, "hover", |el, page| async move {
      actions::hover_with_opts(&el, page, opts_ref).await
    })
  }

  /// Focus the element matched by this locator.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found.
  pub async fn focus(&self) -> Result<()> {
    retry_resolve!(
      self,
      ::std::option::Option::<u64>::None,
      "focus",
      |el, _page| async move {
        el.call_js_fn("function() { this.focus(); }").await?;
        Ok::<(), crate::error::FerriError>(())
      }
    )
  }

  /// Check a checkbox or radio button if it is not already checked.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or is not actionable.
  pub async fn check(&self, opts: Option<crate::options::CheckOptions>) -> Result<()> {
    self.set_checked(true, opts).await
  }

  /// Uncheck a checkbox if it is currently checked.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or is not actionable.
  pub async fn uncheck(&self, opts: Option<crate::options::CheckOptions>) -> Result<()> {
    self.set_checked(false, opts).await
  }

  /// Set the checked state of a checkbox or radio button to match
  /// `checked`. Reads the element's current `checked` property; if it
  /// already matches the target state, the call is a no-op (but
  /// actionability checks still run). Otherwise dispatches a real click
  /// via [`actions::click_with_opts`] so the page sees `input` /
  /// `change` events with the correct timing.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found, is not
  /// actionable, or the click dispatch fails.
  pub async fn set_checked(&self, checked: bool, opts: Option<crate::options::CheckOptions>) -> Result<()> {
    let opts = opts.unwrap_or_default();
    let trial = opts.is_trial();
    // Lower to ClickOptions for the shared click dispatch path so
    // `force` / `trial` / `position` / `timeout` all flow through.
    let click_opts = opts.into_click_options();
    let click_opts_ref = &click_opts;
    retry_resolve!(self, click_opts_ref.timeout, "check", |el, page| async move {
      // setChecked flow:
      //   1. Read current checked state (via `fd.getChecked`, which
      //      understands `input[type=checkbox|radio]` AND ARIA
      //      `aria-checked` roles — `this.checked` alone misses the
      //      latter).
      //   2. If current already matches target → done, no click.
      //   3. Uncheck of a checked radio → hard error (radios only
      //      toggle off by selecting another in their group).
      //   4. Dispatch the click with the caller's options.
      //   5. If `trial` → done (skip verification).
      //   6. Re-read state; if it still doesn't match the target →
      //      `"Clicking the checkbox did not change its state"`.
      let fd = page.injected_script().await?;
      let state_js = format!(
        "function() {{ \
           var r = {fd}.getChecked(this); \
           var isRadio = this.nodeName === 'INPUT' && this.type === 'radio'; \
           return JSON.stringify({{ state: r, isRadio: isRadio }}); \
         }}"
      );
      let read_state = async || -> crate::error::Result<(Option<bool>, bool)> {
        let raw = el
          .call_js_fn_value(&state_js)
          .await?
          .and_then(|v| v.as_str().map(std::string::ToString::to_string))
          .unwrap_or_default();
        let parsed: serde_json::Value = serde_json::from_str(&raw).unwrap_or(serde_json::json!({}));
        let is_radio = parsed
          .get("isRadio")
          .and_then(serde_json::Value::as_bool)
          .unwrap_or(false);
        let state_val = match parsed.get("state") {
          Some(v) if v.is_boolean() => Some(v.as_bool().unwrap_or(false)),
          _ => None,
        };
        Ok((state_val, is_radio))
      };

      let (current, is_radio) = read_state().await?;
      let Some(current) = current else {
        return Err(crate::error::FerriError::invalid_argument(
          "element",
          "not a checkbox, radio button, or ARIA-checkable element",
        ));
      };
      if current == checked {
        return Ok::<(), crate::error::FerriError>(());
      }
      if !checked && is_radio {
        return Err(crate::error::FerriError::invalid_argument(
          "element",
          "Cannot uncheck radio button. Radio buttons can only be unchecked by selecting another radio button in the same group.",
        ));
      }
      actions::click_with_opts(&el, page, click_opts_ref).await?;
      if trial {
        return Ok::<(), crate::error::FerriError>(());
      }
      let (new_state, _) = read_state().await?;
      if new_state != Some(checked) {
        return Err(crate::error::FerriError::backend(
          "clicking the checkbox did not change its state",
        ));
      }
      Ok::<(), crate::error::FerriError>(())
    })
  }

  /// Tap the element (touch event). Dispatches touchstart + touchend on platforms
  /// that support Touch/TouchEvent APIs, falls back to pointerdown + pointerup + click
  /// on desktop browsers (e.g. Playwright `WebKit`) where Touch constructors are unavailable.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or the tap event dispatch fails.
  pub async fn tap(&self, opts: Option<crate::options::TapOptions>) -> Result<()> {
    let opts = opts.unwrap_or_default();
    let opts_ref = &opts;
    retry_resolve!(self, opts_ref.timeout, "tap", |el, page| async move {
      actions::tap_with_opts(&el, page, opts_ref).await
    })
  }

  /// Select all text in an input or textarea element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or the selection fails.
  pub async fn select_text(&self) -> Result<()> {
    let el = self.resolve().await?;
    el.call_js_fn(
      "function() { \
      this.focus(); \
      if (this.select) { this.select(); } \
      else if (this.setSelectionRange) { this.setSelectionRange(0, this.value ? this.value.length : 0); } \
    }",
    )
    .await
  }

  /// Select an `<option>` by value within a `<select>` element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or is not a `<select>`.
  pub async fn select_option(
    &self,
    values: Vec<crate::options::SelectOptionValue>,
    opts: Option<crate::options::SelectOptionOptions>,
  ) -> Result<Vec<String>> {
    let opts = opts.unwrap_or_default();
    let timeout_ms = opts.timeout;
    let force = opts.force.unwrap_or(false);
    let values_ref = &values;
    // Mirrors Playwright's `server/dom.ts::_selectOption`: when not
    // `force`, gate the dispatch on `checkElementStates(['visible',
    // 'enabled'])` so a hidden or disabled `<select>` returns the
    // `error:not<state>` retriable marker until the deadline fires.
    // `force: true` skips the pre-check and goes straight to the
    // injected `selectOptions` call.
    retry_resolve!(self, timeout_ms, "selectOption", |el, page| async move {
      if !force {
        let fd = page.injected_script().await?;
        let state_raw = el
          .call_js_fn_value(&format!(
            "function() {{ return {fd}.checkElementStates(this, ['visible', 'enabled']); }}"
          ))
          .await?
          .and_then(|v| v.as_str().map(std::string::ToString::to_string))
          .unwrap_or_else(|| "error:notconnected".to_string());
        if state_raw != "done" {
          return Err(crate::error::FerriError::backend(state_raw));
        }
      }
      actions::select_options(&el, page, values_ref).await
    })
  }

  /// Set file paths on a file input element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element is not a file input or the upload fails.
  pub async fn set_input_files(
    &self,
    files: crate::options::InputFiles,
    _opts: Option<crate::options::SetInputFilesOptions>,
  ) -> Result<()> {
    // Lower `Payloads` to temp-file paths so the wire-level CDP
    // `DOM.setFileInputFiles` command can carry them unchanged — the
    // alternative would be a separate per-backend `setFileInputBytes`
    // op, which only Playwright's internal CDP protocol supports.
    // Temp files live for the action only; we delete them after the
    // backend call returns regardless of success/failure.
    match files {
      crate::options::InputFiles::Paths(paths) => {
        let strs: Vec<String> = paths.into_iter().map(|p| p.display().to_string()).collect();
        actions::upload_file(self.frame.page_arc().inner(), &self.selector, &strs).await
      },
      crate::options::InputFiles::Payloads(payloads) => {
        // Each payload gets its own subdirectory so the filename on
        // disk matches `payload.name` verbatim — otherwise the page
        // would see a ferridriver-internal `{i}-` prefix and
        // duplicate names would collide in the shared temp root.
        // Matches Playwright's `setInputFilePaths` server path which
        // materialises each payload to a temporary directory unique
        // to the upload.
        //
        // We deliberately DO NOT delete these temp files after
        // `upload_file` returns. CDP's `DOM.setFileInputFiles` only
        // records the paths on the `<input>`; the browser does not
        // actually read file content until the page JS calls
        // `input.files[i].size` / `reader.readAsText(...)` — which
        // happens AFTER this function returns. Deleting on the
        // success path leaves the page with zero-byte files (the
        // handle survives but the backing file is gone). The
        // process-scoped root is cleaned up by the OS on reboot and
        // the per-upload subdirs share that root, so we don't leak
        // indefinitely across a test run.
        let tmp_root = std::env::temp_dir().join(format!("ferridriver-files-{}", std::process::id()));
        std::fs::create_dir_all(&tmp_root)
          .map_err(|e| crate::error::FerriError::Backend(format!("failed to create upload temp dir: {e}")))?;
        let upload_id = std::time::SystemTime::now()
          .duration_since(std::time::UNIX_EPOCH)
          .map_or(0, |d| d.as_nanos());
        let mut paths: Vec<String> = Vec::new();
        for (i, p) in payloads.iter().enumerate() {
          let sub = tmp_root.join(format!("{upload_id}-{i}"));
          std::fs::create_dir_all(&sub)
            .map_err(|e| crate::error::FerriError::Backend(format!("failed to create payload subdir: {e}")))?;
          let safe_name = p.name.replace(['/', '\\', '\0'], "_");
          let path = sub.join(&safe_name);
          std::fs::write(&path, &p.buffer)
            .map_err(|e| crate::error::FerriError::Backend(format!("failed to write upload payload: {e}")))?;
          paths.push(path.display().to_string());
        }
        actions::upload_file(self.frame.page_arc().inner(), &self.selector, &paths).await
      },
    }
  }

  /// Scroll the element into the visible area of the viewport.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or scroll fails.
  pub async fn scroll_into_view_if_needed(&self) -> Result<()> {
    let el = self.resolve().await?;
    el.scroll_into_view().await
  }

  /// Dispatch a DOM event of the given type on the element. Mirrors
  /// Playwright's `frames.ts::dispatchEvent` (see
  /// `/tmp/playwright/packages/playwright-core/src/server/frames.ts:847`):
  /// resolve the element under the retry loop (Playwright does NOT run
  /// actionability for dispatchEvent — it's a programmatic dispatch),
  /// then invoke `injectedScript.dispatchEvent` with the matching
  /// constructor. `opts.timeout` flows through to the retry deadline.
  ///
  /// # Errors
  ///
  /// Returns `FerriError::Timeout` if the element does not appear
  /// before the deadline.
  pub async fn dispatch_event(
    &self,
    event_type: &str,
    event_init: Option<serde_json::Value>,
    opts: Option<crate::options::DispatchEventOptions>,
  ) -> Result<()> {
    let timeout_ms = opts.and_then(|o| o.timeout);
    let init_json = event_init.as_ref().map_or_else(
      || "{}".to_string(),
      |v| serde_json::to_string(v).unwrap_or_else(|_| "{}".to_string()),
    );
    // Escape `</script>` which would break our JS formatting if
    // event_init contained a close-script sequence.
    let init_js = init_json.replace("</", "<\\/");
    let js = format!(
      "function() {{ \
        var type = '{event_type}'; \
        var init = Object.assign({{bubbles: true, cancelable: true, composed: true}}, {init_js}); \
        var ev; \
        if (['click','dblclick','mousedown','mouseup','mouseenter','mouseleave','mousemove','mouseover','mouseout','contextmenu','auxclick'].includes(type)) {{ \
          ev = new MouseEvent(type, init); \
        }} else if (['keydown','keyup','keypress'].includes(type)) {{ \
          ev = new KeyboardEvent(type, init); \
        }} else if (['touchstart','touchend','touchmove','touchcancel'].includes(type) && typeof TouchEvent !== 'undefined') {{ \
          ev = new TouchEvent(type, init); \
        }} else if (['pointerdown','pointerup','pointermove','pointerover','pointerout','pointerenter','pointerleave','pointercancel','gotpointercapture','lostpointercapture'].includes(type)) {{ \
          ev = new PointerEvent(type, init); \
        }} else if (['dragstart','drag','dragenter','dragleave','dragover','drop','dragend'].includes(type)) {{ \
          ev = new DragEvent(type, init); \
        }} else if (['focus','blur','focusin','focusout'].includes(type)) {{ \
          ev = new FocusEvent(type, init); \
        }} else if (['input','beforeinput'].includes(type)) {{ \
          ev = new InputEvent(type, init); \
        }} else if (type === 'wheel') {{ \
          ev = new WheelEvent(type, init); \
        }} else if (['deviceorientation','deviceorientationabsolute'].includes(type)) {{ \
          ev = new DeviceOrientationEvent(type, init); \
        }} else {{ \
          ev = new Event(type, init); \
        }} \
        this.dispatchEvent(ev); \
      }}"
    );
    let js_ref = js.as_str();
    retry_resolve!(self, timeout_ms, "dispatchEvent", |el, _page| async move {
      el.call_js_fn(js_ref).await
    })
  }

  // ── Content & state ───────────────────────────────────────────────────────

  /// Return the `textContent` of the element, or `None` if not found.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn text_content(&self) -> Result<Option<String>> {
    self.eval_prop("textContent").await
  }

  /// Return the `innerText` of the element.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn inner_text(&self) -> Result<String> {
    self
      .eval_prop("innerText")
      .await
      .map(std::option::Option::unwrap_or_default)
  }

  /// Return the `innerHTML` of the element.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn inner_html(&self) -> Result<String> {
    self
      .eval_prop("innerHTML")
      .await
      .map(std::option::Option::unwrap_or_default)
  }

  /// Playwright: `locator.ariaSnapshot(options?: TimeoutOptions &
  /// { mode?: 'ai' | 'default', depth?: number }): Promise<string>`
  /// (`/tmp/playwright/packages/playwright-core/src/client/locator.ts:327`).
  ///
  /// Resolves this locator to a single element under the auto-wait /
  /// retry pipeline (strict mode honored — strictness + actionability
  /// stay in Rust core), then renders the accessibility subtree rooted
  /// at that element via the vendored Playwright `InjectedScript`
  /// (`window.__fd.incrementalAriaSnapshot`). The output is
  /// byte-for-byte the Playwright YAML, scoped to the element — siblings
  /// outside the locator are excluded by construction.
  ///
  /// Cross-iframe: when the subtree contains `<iframe>` nodes that the
  /// renderer assigned refs to (i.e. `mode: 'ai'` — `mode: 'default'`
  /// emits no refs, exactly like Playwright, so there is nothing to
  /// stitch), the child browsing contexts are snapshotted recursively
  /// and spliced under their `- iframe [ref=...]` line, mirroring
  /// `ariaSnapshotForFrame` / `ariaSnapshotFrameRef`
  /// (`/tmp/playwright/.../server/page.ts:1103`). Each frame gets a
  /// unique `fN` ref-prefix so refs never collide across frames.
  /// Uniform across every backend (same vendored renderer + the same
  /// content-frame resolution `frameLocator` uses).
  ///
  /// # Errors
  ///
  /// [`crate::error::FerriError::Timeout`] if the element cannot be
  /// resolved within the timeout; forwards the page-side render error.
  pub async fn aria_snapshot(&self, options: crate::options::AriaSnapshotOptions) -> Result<String> {
    // The frame the element resolves in (frameLocator enter-frame hops
    // resolved here) — root for the recursive child-iframe descent.
    let (root_frame, _sel) = self.resolved().await?;
    let mode = options.mode.unwrap_or_default().as_str();
    let depth = options.depth;
    let opts_json = aria_opts_json(mode, depth, "");
    let root_js =
      format!("function() {{ return JSON.stringify(window.__fd.incrementalAriaSnapshot(this, {opts_json})); }}");
    retry_resolve!(self, options.timeout, "ariaSnapshot", |el, _page| async {
      let raw_s = el
        .call_js_fn_value(&root_js)
        .await?
        .and_then(|v| v.as_str().map(std::string::ToString::to_string))
        .unwrap_or_default();
      let raw = parse_aria_raw(&raw_s)?;
      let seq = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
      let lines = aria_stitch_frame(root_frame.clone(), raw, mode.to_string(), depth, seq).await?;
      Ok::<String, crate::error::FerriError>(lines.join("\n"))
    })
  }

  /// Get the value of an attribute on the element.
  ///
  /// Returns the raw attribute string exactly as
  /// `Element.getAttribute(name)` reports it (HTML attributes are always
  /// `string | null` per DOM spec — there is no native numeric/boolean
  /// attribute type). Playwright parity: Playwright's `getAttribute`
  /// returns `Promise<string | null>`; the previous implementation
  /// leaked the JSON-stringified form of non-string JS values (e.g.
  /// `"42"` vs `42`) — that path was unreachable with a well-behaved
  /// browser, but we now explicitly rule it out.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn get_attribute(&self, name: &str) -> Result<Option<String>> {
    let escaped = name.replace('\\', "\\\\").replace('\'', "\\'");
    let val = self
      .eval_on_element(&format!("return el.getAttribute('{escaped}');"))
      .await?;
    Ok(val.and_then(|v| match v {
      serde_json::Value::String(s) => Some(s),
      // Per the DOM spec `Element.getAttribute` only ever returns
      // `string | null`. Anything else coming back from the eval
      // indicates a browser bug or an unexpected injected script —
      // surface as `None` rather than silently JSON-stringifying.
      _ => None,
    }))
  }

  /// Return the `value` property of an input or textarea element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or JS evaluation fails.
  pub async fn input_value(&self) -> Result<String> {
    self
      .eval_prop("value")
      .await
      .map(std::option::Option::unwrap_or_default)
  }

  /// Check whether the element is visible (not `display:none`, `visibility:hidden`,
  /// or `opacity:0`). Returns `false` if the element does not exist.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn is_visible(&self) -> Result<bool> {
    // Single evaluate: find element + check visibility. Returns false if not found.
    let val = self
      .eval_on_element(
        "var s = getComputedStyle(el); \
       return s.display !== 'none' && s.visibility !== 'hidden' && s.opacity !== '0';",
      )
      .await?;
    // eval_on_element returns null if element not found -> false (Playwright behavior)
    Ok(val.and_then(|v| v.as_bool()).unwrap_or(false))
  }

  /// Check whether the element is hidden (inverse of `is_visible`).
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn is_hidden(&self) -> Result<bool> {
    self.is_visible().await.map(|v| !v)
  }

  /// Check whether the element is enabled (i.e. not `disabled`).
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or JS evaluation fails.
  pub async fn is_enabled(&self) -> Result<bool> {
    self.eval_bool("function() { return !this.disabled; }").await
  }

  /// Check whether the element is disabled.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or JS evaluation fails.
  pub async fn is_disabled(&self) -> Result<bool> {
    self.eval_bool("function() { return !!this.disabled; }").await
  }

  /// Check whether a checkbox or radio button is checked.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or JS evaluation fails.
  pub async fn is_checked(&self) -> Result<bool> {
    self.eval_bool("function() { return !!this.checked; }").await
  }

  /// Check if the element is attached to the DOM (exists in the document).
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing fails.
  pub async fn is_attached(&self) -> Result<bool> {
    Ok(self.resolve().await.is_ok())
  }

  /// Count the number of elements matching this locator's selector.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn count(&self) -> Result<usize> {
    // Resolve frameLocator enter-frame hops, then count the trailing
    // selector inside the resolved frame (no-op for plain selectors).
    let (rf, rsel) = self.resolved().await?;
    let parsed = selectors::parse(&rsel)?;
    let parts_json = selectors::build_parts_json(&parsed);
    let inner = rf.page_arc().inner();
    let fd = inner.injected_script().await?;
    let js = format!("{fd}.selCount({parts_json})");
    let val = if rf.is_main_frame() {
      inner.evaluate(&js).await
    } else {
      inner.evaluate_in_frame(&js, rf.id()).await
    }?
    .and_then(|v| v.as_u64())
    .unwrap_or(0);
    Ok(usize::try_from(val).unwrap_or(usize::MAX))
  }

  /// Resolve this locator to a canonical selector and return a new
  /// [`Locator`] built from it.
  ///
  /// Playwright: `normalize(): Promise<Locator>`
  /// (`/tmp/playwright/packages/playwright-core/src/client/locator.ts:269`)
  /// which calls `frame.resolveSelector` -> `injected.generateSelectorSimple`
  /// (`/tmp/playwright/packages/playwright-core/src/server/frames.ts:1274`).
  ///
  /// The trailing selector (the part that runs inside the deepest
  /// resolved frame) is replaced with the recorder/codegen selector the
  /// injected script generates for the single matched element. When the
  /// locator targets a child frame (it carries `internal:control=enter-frame`
  /// hops), the original enter-frame prefix is preserved so the returned
  /// locator still resolves into the same frame; only the trailing
  /// segment is canonicalised. Strict by design: errors if 0 or >1
  /// elements match, mirroring Playwright's `selectors.query`.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing fails, no element matches, or
  /// more than one element matches.
  pub async fn normalize(&self) -> Result<Locator> {
    const MARK: &str = ">> internal:control=enter-frame >>";
    let (rf, rsel) = self.resolved().await?;
    let frame_id: Option<&str> = if rf.is_main_frame() { None } else { Some(rf.id()) };
    let generated = selectors::normalize_selector(rf.page_arc().inner(), &rsel, frame_id).await?;
    // Re-attach the enter-frame prefix (everything up to and including
    // the last hop) so the new locator targets the same frame; only the
    // trailing segment is replaced with the canonical generated selector.
    let new_selector = match self.selector.rsplit_once(MARK) {
      Some((prefix, _)) => format!("{prefix}{MARK} {generated}"),
      None => generated,
    };
    Ok(Locator::new(self.frame.clone(), new_selector))
  }

  /// Return the bounding box of the element, or `None` if the element is not found.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn bounding_box(&self) -> Result<Option<BoundingBox>> {
    let val = self
      .eval_on_element("var r = el.getBoundingClientRect(); return {x:r.x,y:r.y,width:r.width,height:r.height};")
      .await?;
    match val {
      Some(v) => Ok(Some(BoundingBox {
        x: v["x"].as_f64().unwrap_or(0.0),
        y: v["y"].as_f64().unwrap_or(0.0),
        width: v["width"].as_f64().unwrap_or(0.0),
        height: v["height"].as_f64().unwrap_or(0.0),
      })),
      None => Ok(None),
    }
  }

  // ── Waiting ───────────────────────────────────────────────────────────────

  /// Wait for the element to reach the specified state.
  ///
  /// Playwright states (`packages/playwright-core/src/client/locator.ts`):
  ///
  /// * `"attached"` — element is present in the DOM. Computed style is
  ///   not consulted. Matches `element.isConnected`.
  /// * `"visible"` — element is attached **and** has non-empty bounding
  ///   box, is not `display:none` / `visibility:hidden` / `opacity:0`.
  /// * `"hidden"` — element is either detached or not visible. A
  ///   detached element satisfies `"hidden"` (Playwright parity).
  /// * `"detached"` — element is not present in the DOM.
  ///
  /// Previously `"attached"` and `"visible"` were conflated — both
  /// returned as soon as a DOM query succeeded. That broke Playwright
  /// tests that rely on `attached` resolving for zero-size or
  /// currently-invisible elements.
  ///
  /// # Errors
  ///
  /// Returns an error if the timeout expires before the element reaches
  /// the desired state, or if an unknown state is specified.
  pub async fn wait_for(&self, opts: WaitOptions) -> Result<()> {
    let timeout = opts.timeout.unwrap_or(30000);
    let state = opts.state.as_deref().unwrap_or("visible");
    let deadline = tokio::time::Instant::now() + std::time::Duration::from_millis(timeout);

    loop {
      if tokio::time::Instant::now() >= deadline {
        return Err(crate::error::FerriError::timeout(
          format!("waiting for '{}' to be {state}", self.selector),
          timeout,
        ));
      }
      match state {
        "attached" => {
          // Only require DOM presence — do not consult computed style.
          if selectors::query_one(
            self.frame.page_arc().inner(),
            &self.selector,
            false,
            if self.frame.is_main_frame() {
              None
            } else {
              Some(self.frame.id())
            },
          )
          .await
          .is_ok()
          {
            selectors::cleanup_tags(self.frame.page_arc().inner()).await;
            return Ok(());
          }
        },
        "visible" => {
          // DOM presence AND computed-style visible. Fail silently
          // (fall through to next poll) if `is_visible()` errors
          // because the element is detached mid-poll.
          if let Ok(true) = self.is_visible().await {
            return Ok(());
          }
        },
        "detached" => {
          if selectors::query_one(
            self.frame.page_arc().inner(),
            &self.selector,
            false,
            if self.frame.is_main_frame() {
              None
            } else {
              Some(self.frame.id())
            },
          )
          .await
          .is_err()
          {
            return Ok(());
          }
          selectors::cleanup_tags(self.frame.page_arc().inner()).await;
        },
        "hidden" => {
          // Playwright: `hidden` is satisfied by detachment OR by the
          // element being present but not visible.
          if selectors::query_one(
            self.frame.page_arc().inner(),
            &self.selector,
            false,
            if self.frame.is_main_frame() {
              None
            } else {
              Some(self.frame.id())
            },
          )
          .await
          .is_err()
          {
            return Ok(());
          }
          selectors::cleanup_tags(self.frame.page_arc().inner()).await;
          if let Ok(false) = self.is_visible().await {
            return Ok(());
          }
        },
        _ => {
          return Err(crate::error::FerriError::invalid_argument(
            "state",
            format!("unknown wait state: {state}"),
          ));
        },
      }
      tokio::time::sleep(std::time::Duration::from_millis(50)).await;
    }
  }

  // ── Screenshot ────────────────────────────────────────────────────────────

  /// Take a PNG screenshot of the element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or screenshot capture fails.
  pub async fn screenshot(&self) -> Result<Vec<u8>> {
    let el = self.resolve().await?;
    el.screenshot(crate::backend::ImageFormat::Png).await
  }

  // ── Editable check ───────────────────────────────────────────────────────

  /// Check whether the element is editable (not disabled and not read-only).
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or JS evaluation fails.
  pub async fn is_editable(&self) -> Result<bool> {
    self
      .eval_bool("function() { return !this.disabled && !this.readOnly; }")
      .await
  }

  // ── Blur ────────────────────────────────────────────────────────────────

  /// Remove focus from the element.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found.
  pub async fn blur(&self) -> Result<()> {
    let el = self.resolve().await?;
    let _ = el.call_js_fn("function() { this.blur(); }").await;
    Ok(())
  }

  // ── Press sequentially ──────────────────────────────────────────────────

  /// Type text character by character with a delay between each.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be found or any key press fails.
  pub async fn press_sequentially(&self, text: &str, opts: Option<crate::options::TypeOptions>) -> Result<()> {
    // Playwright's `pressSequentially` shares the `TypeOptions` shape
    // with deprecated `type` (same three fields), so route both here.
    self.r#type(text, opts).await
  }

  // ── Drag to another locator ─────────────────────────────────────────────

  /// Drag this element to `target`. Mirrors Playwright's
  /// `Locator.dragTo(target, options)` signature per
  /// `/tmp/playwright/packages/playwright-core/types/types.d.ts:13293`.
  ///
  /// When `DragAndDropOptions::source_position` is set, the press point is
  /// the source element's padding-box origin offset by that point; otherwise
  /// the source element's center is used. Same for `target_position` on the
  /// release point. `DragAndDropOptions::steps` controls how many
  /// interpolated `mousemove` events are emitted between press and release
  /// (Playwright default: `1`). `DragAndDropOptions::trial` skips the
  /// actual mouse action, returning after both elements resolve.
  /// `DragAndDropOptions::strict` is ignored here (per Playwright) because
  /// this locator already carries its own strict flag.
  ///
  /// # Errors
  ///
  /// Returns an error if either element cannot be found, bounding box
  /// coordinates cannot be read, or the drag operation fails.
  pub async fn drag_to(&self, target: &Locator, options: Option<crate::options::DragAndDropOptions>) -> Result<()> {
    let opts = options.unwrap_or_default();

    // Get source + target geometry via call_js_fn_value (1 CDP each).
    let source_el = self.resolve().await?;
    let target_el = target.resolve().await?;

    // Parallel: get both bounding rects simultaneously — we need the full
    // rect (x, y, width, height) so that sourcePosition / targetPosition can
    // be offset from the padding-box origin as Playwright does.
    let (src_result, tgt_result) = tokio::join!(
      source_el.call_js_fn_value(
        "function() { try { this.scrollIntoViewIfNeeded(); } catch (e) { this.scrollIntoView(); } var r = this.getBoundingClientRect(); return {x: r.x, y: r.y, width: r.width, height: r.height}; }"
      ),
      target_el.call_js_fn_value(
        "function() { try { this.scrollIntoViewIfNeeded(); } catch (e) { this.scrollIntoView(); } var r = this.getBoundingClientRect(); return {x: r.x, y: r.y, width: r.width, height: r.height}; }"
      ),
    );

    let src = src_result?.ok_or_else(|| crate::error::FerriError::Backend("no source bounding box".into()))?;
    let tgt = tgt_result?.ok_or_else(|| crate::error::FerriError::Backend("no target bounding box".into()))?;

    let from = rect_point(&src, opts.source_position);
    let to = rect_point(&tgt, opts.target_position);

    // Playwright's `trial: true` performs actionability checks (resolve) and
    // skips the actual action. We've already resolved both elements above,
    // so simply return without dispatching mouse events.
    if opts.trial.unwrap_or(false) {
      return Ok(());
    }

    let steps = opts.steps.unwrap_or(1);
    self.frame.page_arc().inner().click_and_drag(from, to, steps).await
  }

  // ── Drop a payload onto this element ────────────────────────────────────

  /// Drop a file/data payload onto this element. Mirrors Playwright's
  /// `Locator.drop(payload, options)` (`client/locator.ts:129`), which
  /// forwards to `frame._drop` -> server `dom.ts::_drop`.
  ///
  /// The drop is performed by constructing a `DataTransfer` carrying the
  /// payload's `File` objects (built from each `FilePayload`'s bytes) and
  /// `data` entries (`DataTransfer.setData(mimeType, value)`), then
  /// dispatching the `dragenter` / `dragover` / `drop` `DragEvent`
  /// sequence on the resolved element at the drop point. Matching
  /// Playwright, if the `dragover` handler does not call `preventDefault()`
  /// the target is treated as rejecting the drop: a `dragleave` is
  /// dispatched and a `FerriError::Backend` is returned.
  ///
  /// `DropOptions::position` offsets the drop point from the element's
  /// padding-box top-left; when absent the element center is used.
  /// `DropOptions::modifiers` are reflected on the dispatched `DragEvent`s'
  /// modifier flags. `DropOptions::timeout` is accepted for signature
  /// parity; the underlying single-shot resolve already honours the
  /// context's default action timeout.
  ///
  /// File paths in `DropPayload::files` are read into memory here (matching
  /// the co-located server path in Playwright) so the page can construct
  /// real `File` objects without filesystem access of its own.
  ///
  /// # Errors
  ///
  /// Returns an error if the element cannot be resolved, a referenced file
  /// path cannot be read, or the target rejects the drop.
  pub async fn drop(
    &self,
    payload: crate::options::DropPayload,
    options: Option<crate::options::DropOptions>,
  ) -> Result<()> {
    let opts = options.unwrap_or_default();

    // Lower the payload's files into `{name, mimeType, buffer(base64)}`
    // records the page can rebuild into `File` objects. `Paths` are read
    // from disk into buffers (Playwright's co-located server reads
    // localPaths the same way); `Payloads` carry their bytes already.
    let file_records = lower_drop_files(payload.files)?;

    let data_records: Vec<serde_json::Value> = payload
      .data
      .into_iter()
      .map(|(mime_type, value)| serde_json::json!({ "mimeType": mime_type, "value": value }))
      .collect();

    let modifiers = serde_json::json!({
      "alt": opts.modifiers.contains(&crate::options::Modifier::Alt),
      "ctrl": opts.modifiers.iter().any(|m| {
        matches!(m, crate::options::Modifier::Control)
          || (matches!(m, crate::options::Modifier::ControlOrMeta) && !cfg!(target_os = "macos"))
      }),
      "meta": opts.modifiers.iter().any(|m| {
        matches!(m, crate::options::Modifier::Meta)
          || (matches!(m, crate::options::Modifier::ControlOrMeta) && cfg!(target_os = "macos"))
      }),
      "shift": opts.modifiers.contains(&crate::options::Modifier::Shift),
    });

    let position = match opts.position {
      Some(p) => serde_json::json!({ "x": p.x, "y": p.y }),
      None => serde_json::Value::Null,
    };

    let arg = serde_json::json!({
      "payloads": file_records,
      "data": data_records,
      "modifiers": modifiers,
      "position": position,
    });
    let arg_json = serde_json::to_string(&arg)
      .map_err(|e| crate::error::FerriError::Backend(format!("failed to serialise drop payload: {e}")))?;

    let el = self.resolve().await?;
    let function = format!("function() {{ const arg = {arg_json}; {DROP_BODY} }}");
    let result = el.call_js_fn_value(&function).await?;

    match result.as_ref().and_then(serde_json::Value::as_str) {
      Some("accepted") => Ok(()),
      Some("not-accepted") => Err(crate::error::FerriError::Backend(
        "Drop target did not accept the drop -- its dragover handler did not call preventDefault()".into(),
      )),
      Some("error:notconnected") => Err(crate::error::FerriError::Backend(
        "Drop target element is not connected to the document".into(),
      )),
      _ => Err(crate::error::FerriError::Backend(
        "drop did not return a recognised status".into(),
      )),
    }
  }

  // ── Combinators ─────────────────────────────────────────────────────────

  /// Union: matches elements from either this or the other locator.
  /// Creates a new locator that matches elements matched by **either**
  /// selector. Mirrors Playwright's `Locator.or(locator)` exactly: emits
  /// `>> internal:or=<json>` where the injected selector engine handles the
  /// union.
  ///
  /// Unlike CSS `:is()`, this works for every selector engine including
  /// `text=`, `role=`, `label=`, `testid=`, and chained rich selectors.
  #[must_use]
  pub fn or(&self, other: &Locator) -> Locator {
    self.chain(&format!(
      "internal:or={}",
      serde_json::to_string(&other.selector).unwrap_or_else(|_| format!("{:?}", other.selector))
    ))
  }

  /// Creates a new locator that matches elements matched by **both** this
  /// locator and `other` on the same element. Mirrors Playwright's
  /// `Locator.and(locator)` — emits `>> internal:and=<json>`.
  ///
  /// This is a fundamentally different operation from `locator.locator(...)`
  /// which narrows scope to descendants; `and` requires the same element to
  /// satisfy both selectors.
  #[must_use]
  pub fn and(&self, other: &Locator) -> Locator {
    self.chain(&format!(
      "internal:and={}",
      serde_json::to_string(&other.selector).unwrap_or_else(|_| format!("{:?}", other.selector))
    ))
  }

  // ── All matches ─────────────────────────────────────────────────────────

  /// Return all matching locators as individual Locator instances.
  ///
  /// # Errors
  ///
  /// Returns an error if the count query fails due to selector parsing
  /// or JS evaluation errors.
  pub async fn all(&self) -> Result<Vec<Locator>> {
    let count = self.count().await?;
    let mut locators = Vec::with_capacity(count);
    let base = &self.selector;
    for i in 0..count {
      let idx = i32::try_from(i).unwrap_or(i32::MAX);
      let selector = if base.is_empty() {
        format!("nth={idx}")
      } else {
        format!("{base} >> nth={idx}")
      };
      locators.push(Locator {
        frame: self.frame.clone(),
        selector,
        strict: true,
      });
    }
    Ok(locators)
  }

  /// Get text content of all matching elements.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn all_text_contents(&self) -> Result<Vec<String>> {
    let parsed = selectors::parse(&self.selector)?;
    let parts_json = selectors::build_parts_json(&parsed);
    self.frame.page_arc().inner().ensure_engine_injected().await?;
    let fd = "window.__fd";
    let js = format!(
      "(function() {{ var r = {fd}._exec({parts_json}, document); \
       return r.map(function(e) {{ return (e.textContent || '').trim(); }}); }})()"
    );
    let val = self.frame.page_arc().inner().evaluate(&js).await?;
    match val {
      Some(serde_json::Value::Array(arr)) => Ok(
        arr
          .into_iter()
          .filter_map(|v| v.as_str().map(std::string::ToString::to_string))
          .collect(),
      ),
      _ => Ok(Vec::new()),
    }
  }

  /// Get inner text of all matching elements.
  ///
  /// # Errors
  ///
  /// Returns an error if selector parsing or JS evaluation fails.
  pub async fn all_inner_texts(&self) -> Result<Vec<String>> {
    // Same as all_text_contents for our implementation
    self.all_text_contents().await
  }

  // ── Evaluate (Playwright parity) ─────────────────────────────────────

  /// Playwright: `locator.evaluate(pageFunction, arg?, options?): Promise<R>`
  /// (`/tmp/playwright/packages/playwright-core/src/client/locator.ts:129`).
  ///
  /// Resolves this locator's element under the auto-wait / retry
  /// pipeline, then calls `fn(element, arg)` in the page context.
  /// Disposes the intermediate handle before returning.
  ///
  /// # Errors
  ///
  /// Returns [`crate::error::FerriError::Timeout`] when the element
  /// cannot be resolved within the configured timeout, or forwards
  /// the page-side evaluate error.
  pub async fn evaluate(
    &self,
    fn_source: &str,
    arg: crate::protocol::SerializedArgument,
    is_function: Option<bool>,
    options: Option<crate::options::EvaluateOptions>,
  ) -> Result<crate::protocol::SerializedValue> {
    let timeout_ms = options.and_then(|o| o.timeout);
    let fn_source = fn_source.to_string();
    retry_resolve!(self, timeout_ms, "evaluate", |el, _page| async {
      let page_arc = Arc::clone(self.frame.page_arc());
      let handle = crate::element_handle::ElementHandle::from_any_element(page_arc, el).await?;
      let result = handle
        .as_js_handle()
        .evaluate(&fn_source, arg.clone(), is_function)
        .await;
      let _ = handle.dispose().await;
      result
    })
  }

  /// Playwright: `locator.evaluateHandle(pageFunction, arg?, options?): Promise<JSHandle>`
  /// (`/tmp/playwright/packages/playwright-core/src/client/locator.ts:138`).
  ///
  /// Resolves this locator's element under auto-wait / retry, then calls
  /// `fn(element, arg)` retaining the result on the page and returning
  /// it as a [`crate::js_handle::JSHandle`]. The intermediate
  /// `ElementHandle` is disposed — the returned handle is an independent
  /// remote reference.
  ///
  /// # Errors
  ///
  /// See [`Self::evaluate`].
  pub async fn evaluate_handle(
    &self,
    fn_source: &str,
    arg: crate::protocol::SerializedArgument,
    is_function: Option<bool>,
    options: Option<crate::options::EvaluateOptions>,
  ) -> Result<crate::js_handle::JSHandle> {
    let timeout_ms = options.and_then(|o| o.timeout);
    let fn_source = fn_source.to_string();
    retry_resolve!(self, timeout_ms, "evaluateHandle", |el, _page| async {
      let page_arc = Arc::clone(self.frame.page_arc());
      let handle = crate::element_handle::ElementHandle::from_any_element(page_arc, el).await?;
      let result = handle
        .as_js_handle()
        .evaluate_handle(&fn_source, arg.clone(), is_function)
        .await;
      let _ = handle.dispose().await;
      result
    })
  }

  /// Playwright: `locator.evaluateAll(pageFunction, arg?): Promise<R>`
  /// (`/tmp/playwright/packages/playwright-core/src/client/locator.ts:133`).
  ///
  /// Resolves every matching element in this locator's frame and calls
  /// `fn(elements, arg)` with the array as the first argument. Unlike
  /// [`Self::evaluate`], no retry/auto-wait — empty matches produce an
  /// empty array (Playwright parity).
  ///
  /// # Errors
  ///
  /// Forwards page-side evaluate error.
  pub async fn evaluate_all(
    &self,
    fn_source: &str,
    arg: crate::protocol::SerializedArgument,
    is_function: Option<bool>,
  ) -> Result<crate::protocol::SerializedValue> {
    let parsed = selectors::parse(&self.selector)?;
    let parts_json = selectors::build_parts_json(&parsed);
    self.frame.page_arc().inner().ensure_engine_injected().await?;
    let probe = format!("() => window.__fd.selAll({parts_json})");
    let array_handle = self
      .frame
      .evaluate_handle(&probe, crate::protocol::SerializedArgument::default(), Some(true))
      .await?;
    let result = array_handle.evaluate(fn_source, arg, is_function).await;
    let _ = array_handle.dispose().await;
    result
  }

  /// Run a value-returning JS expression in this locator's frame.
  /// Uses `evaluate_in_frame` for non-main frames (CDP `contextId`,
  /// `BiDi` realm) and the no-context default for the main frame so we
  /// avoid an extra `frame_contexts` lookup on the hot path.
  async fn evaluate_in_frame_js(&self, js: &str) -> Result<Option<serde_json::Value>> {
    let inner = self.frame.page_arc().inner();
    if self.frame.is_main_frame() {
      inner.evaluate(js).await
    } else {
      inner.evaluate_in_frame(js, self.frame.id()).await
    }
  }

  // ── Page / Frame access ────────────────────────────────────────────────────

  /// Get the page this locator belongs to.
  #[must_use]
  pub fn page(&self) -> &Arc<crate::page::Page> {
    self.frame.page_arc()
  }

  /// The frame this locator resolves in. Mirrors Playwright's
  /// `locator._frame` — actions and queries always run in this frame's
  /// execution context.
  #[must_use]
  pub fn frame(&self) -> &crate::frame::Frame {
    &self.frame
  }

  /// Treat this locator as an `<iframe>` and return a `FrameLocator` for its content.
  ///
  /// Equivalent to Playwright's `locator.contentFrame()`. The returned
  /// `FrameLocator` creates locators scoped to the iframe's content document.
  #[must_use]
  pub fn content_frame(&self) -> FrameLocator {
    FrameLocator::for_iframe_in(self.frame.clone(), self.selector.clone())
  }

  /// Create a `FrameLocator` targeting an `<iframe>` matched by `selector` within
  /// this locator's scope.
  ///
  /// Equivalent to Playwright's `locator.frameLocator(selector)`.
  #[must_use]
  pub fn frame_locator(&self, selector: &str) -> FrameLocator {
    let frame_selector = if self.selector.is_empty() {
      selector.to_string()
    } else {
      format!("{} >> {selector}", self.selector)
    };
    FrameLocator::for_iframe_in(self.frame.clone(), frame_selector)
  }

  // ── Selector access ───────────────────────────────────────────────────────

  #[must_use]
  pub fn selector(&self) -> &str {
    &self.selector
  }

  // ── Handle materialisation (Playwright `locator.elementHandle`) ────

  /// Playwright: `locator.elementHandle(opts?): Promise<ElementHandle>`.
  /// Resolves this locator's selector once and returns a pinned
  /// [`crate::element_handle::ElementHandle`]. Throws when the
  /// selector matches no element — Playwright's behaviour (Playwright
  /// returns `null` from `$` but `elementHandle()` errors on miss).
  ///
  /// Phase-F MVP: no auto-wait — calls into the selector engine
  /// directly. Auto-wait + actionability are a phase-future addition
  /// once the locator's `retry_resolve!` macro is generalised to
  /// return an `ElementHandle` instead of running an action body.
  ///
  /// # Errors
  ///
  /// Returns [`crate::error::FerriError`] on selector parse failure,
  /// missing match, or strict-mode violation.
  pub async fn element_handle(&self) -> crate::error::Result<crate::element_handle::ElementHandle> {
    let page = self.frame.page_arc();
    page.inner().ensure_engine_injected().await?;
    let frame_id: Option<&str> = if self.frame.is_main_frame() {
      None
    } else {
      Some(self.frame.id())
    };
    let element = crate::selectors::query_one(page.inner(), &self.selector, self.strict, frame_id).await?;
    crate::element_handle::ElementHandle::from_any_element(Arc::clone(page), element).await
  }

  /// Playwright: `locator.elementHandles(): Promise<ElementHandle[]>`.
  /// Returns one handle per match in document order.
  ///
  /// # Errors
  ///
  /// Returns [`crate::error::FerriError`] on selector parse / protocol
  /// failure.
  pub async fn element_handles(&self) -> crate::error::Result<Vec<crate::element_handle::ElementHandle>> {
    let page = self.frame.page_arc();
    page.inner().ensure_engine_injected().await?;
    let frame_id: Option<&str> = if self.frame.is_main_frame() {
      None
    } else {
      Some(self.frame.id())
    };
    let matches = crate::selectors::query_all(page.inner(), &self.selector, frame_id).await?;
    let count = matches.len();
    let mut handles = Vec::with_capacity(count);
    for i in 0..count {
      let tagged = format!("window.__fd.selOne([{{\"engine\":\"css\",\"body\":\"[data-fd-sel='{i}']\"}}])");
      match page.inner().evaluate_to_element(&tagged, frame_id).await {
        Ok(element) => {
          handles.push(crate::element_handle::ElementHandle::from_any_element(Arc::clone(page), element).await?);
        },
        Err(err) => {
          crate::selectors::cleanup_tags(page.inner()).await;
          return Err(err);
        },
      }
    }
    crate::selectors::cleanup_tags(page.inner()).await;
    Ok(handles)
  }

  /// Whether this locator runs action methods under strict mode (multi-match
  /// is an error). Mirrors Playwright's default.
  #[must_use]
  pub fn is_strict(&self) -> bool {
    self.strict
  }

  // ── Core retry system ─────────────────────────────────────────────────────
  //
  // Matches Playwright's retryWithProgressAndTimeouts + _retryWithProgressIfNotConnected
  // + _callOnElementOnceMatches. ALL element operations go through one of these two
  // methods. Retry backoff: [0, 20, 50, 100, 100, 500]ms (same as Playwright).

  /// Backoff schedule matching Playwright's retryWithProgressAndTimeouts.
  const RETRY_BACKOFFS_MS: &'static [u64] = &[0, 0, 20, 50, 100, 100, 500];

  /// Resolve element + run JS callback in ONE CDP call, with retry.
  /// Used by: innerText, textContent, innerHTML, getAttribute, inputValue, isVisible, etc.
  /// Matches Playwright's `_callOnElementOnceMatches`.
  async fn retry_eval_on_element(&self, js_body: &str) -> Result<Option<serde_json::Value>> {
    for (i, &delay_ms) in Self::RETRY_BACKOFFS_MS.iter().enumerate() {
      if delay_ms > 0 {
        tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
      }
      // Re-resolve frameLocator enter-frame hops EVERY attempt so a
      // child frame that wasn't loaded / had no execution context yet
      // (or got re-attached) is picked up on a later retry. No-op for
      // plain selectors.
      let attempt: Result<Option<serde_json::Value>> = async {
        let (rf, rsel) = self.resolved().await?;
        let parsed = selectors::parse(&rsel)?;
        let parts_json = selectors::build_parts_json(&parsed);
        let inner = rf.page_arc().inner();
        inner.ensure_engine_injected().await?;
        let fd = "window.__fd";
        let js = format!("(function() {{ var el = {fd}.selOne({parts_json}); if (!el) return null; {js_body} }})()");
        if rf.is_main_frame() {
          inner.evaluate(&js).await
        } else {
          inner.evaluate_in_frame(&js, rf.id()).await
        }
      }
      .await;
      match attempt {
        // Element not found, frame not ready, or eval failed -- retry
        // if attempts remain.
        Ok(Some(serde_json::Value::Null) | None) | Err(_) if i < Self::RETRY_BACKOFFS_MS.len() - 1 => {},
        Ok(val) => return Ok(val),
        Err(e) => return Err(e),
      }
    }
    Ok(None)
  }

  // ── Internal helpers ────────────────────────────────────────────────────────

  /// Resolve the locator to a concrete element.
  ///
  /// # Errors
  ///
  /// Returns an error if the selector engine cannot be injected or the element is not found.
  pub async fn resolve(&self) -> Result<AnyElement> {
    self.frame.page_arc().inner().ensure_engine_injected().await?;
    let fd = "window.__fd";
    let sel_js = selectors::build_selone_js(&self.selector, fd, self.strict)?;
    let frame_id: Option<&str> = if self.frame.is_main_frame() {
      None
    } else {
      Some(self.frame.id())
    };
    selectors::query_one_prebuilt(self.frame.page_arc().inner(), &sel_js, &self.selector, frame_id).await
  }

  fn chain(&self, sub: &str) -> Locator {
    let selector = if self.selector.is_empty() {
      sub.to_string()
    } else {
      format!("{} >> {sub}", self.selector)
    };
    Locator {
      frame: self.frame.clone(),
      selector,
      strict: self.strict,
    }
  }

  async fn eval_prop(&self, prop: &str) -> Result<Option<String>> {
    let val = self
      .retry_eval_on_element(&format!("var v = el.{prop}; return v == null ? null : String(v);"))
      .await?;
    Ok(val.and_then(|v| match v {
      serde_json::Value::String(s) => Some(s),
      serde_json::Value::Null => None,
      other => Some(other.to_string()),
    }))
  }

  async fn eval_bool(&self, func: &str) -> Result<bool> {
    let val = self
      .retry_eval_on_element(&format!("return !!({func}).call(el);"))
      .await?;
    Ok(val.and_then(|v| v.as_bool()).unwrap_or(false))
  }

  /// Legacy: non-retrying eval for callers that handle retry themselves.
  async fn eval_on_element(&self, js_body: &str) -> Result<Option<serde_json::Value>> {
    let parsed = selectors::parse(&self.selector)?;
    let parts_json = selectors::build_parts_json(&parsed);
    self.frame.page_arc().inner().ensure_engine_injected().await?;
    let fd = "window.__fd";
    let js = format!("(function() {{ var el = {fd}.selOne({parts_json}); if (!el) return null; {js_body} }})()");
    self.evaluate_in_frame_js(&js).await
  }
}

impl std::fmt::Debug for Locator {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("Locator")
      .field("selector", &self.selector)
      .field("frame", &self.frame)
      .field("strict", &self.strict)
      .finish()
  }
}

// ── FrameLocator ──────────────────────────────────────────────────────────────

/// A selector-builder that produces parent-frame [`Locator`]s targeting
/// content inside an `<iframe>`. Mirrors Playwright's `FrameLocator`
/// exactly:
///
/// `/tmp/playwright/packages/playwright-core/src/client/locator.ts::FrameLocatorImpl`
///
/// Holds the parent [`crate::Frame`] (the one whose document contains the
/// `<iframe>` element) and the iframe's CSS-selector chain. Every
/// builder method composes a Locator selector with
/// `>> internal:control=enter-frame >>` so the iframe traversal is
/// performed by the selector engine at action time — not eagerly at
/// construction. The resulting `Locator` is the same `Locator` type
/// used everywhere else (no separate iframe-aware locator).
///
/// All methods are synchronous; `internal:control=enter-frame` is the
/// engine-side directive that switches root from the iframe element to
/// its `contentDocument` when a subsequent selector part runs.
#[derive(Clone)]
pub struct FrameLocator {
  /// Parent frame whose document contains the `<iframe>` element.
  /// For top-level `page.frame_locator(sel)` this is the main frame;
  /// nested `frame_locator.frame_locator(sel)` keeps the same parent
  /// frame and just appends to the selector chain.
  frame: crate::frame::Frame,
  /// CSS-selector chain ending at the `<iframe>` element. Composed with
  /// `>> internal:control=enter-frame >>` whenever we step further in.
  frame_selector: String,
}

impl FrameLocator {
  /// Construct a `FrameLocator` for an `<iframe>` matched by
  /// `iframe_selector` inside `parent_frame`'s document. Sync.
  #[must_use]
  pub fn for_iframe_in(parent_frame: crate::frame::Frame, iframe_selector: String) -> Self {
    Self {
      frame: parent_frame,
      frame_selector: iframe_selector,
    }
  }

  fn enter(&self, selector: &str) -> String {
    format!("{} >> internal:control=enter-frame >> {selector}", self.frame_selector)
  }

  /// Locator inside this iframe. Mirrors Playwright's
  /// `frameLocator.locator(selector, options?)` — sync, returns a
  /// `Locator` bound to the parent frame with an `enter-frame`
  /// selector chain.
  #[must_use]
  pub fn locator(&self, selector: &str, options: Option<crate::options::FilterOptions>) -> Locator {
    let base = Locator::new(self.frame.clone(), self.enter(selector));
    match options {
      Some(opts) => base.filter(&opts),
      None => base,
    }
  }

  /// `getByRole` inside this iframe.
  #[must_use]
  pub fn get_by_role(&self, role: &str, opts: &RoleOptions) -> Locator {
    self.locator(&build_role_selector(role, opts), None)
  }

  /// `getByText` inside this iframe.
  #[must_use]
  pub fn get_by_text(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.locator(&build_text_like_selector("internal:text", text, opts), None)
  }

  /// `getByTestId` inside this iframe.
  #[must_use]
  pub fn get_by_test_id(&self, test_id: &StringOrRegex) -> Locator {
    self.locator(&build_testid_selector("data-testid", test_id), None)
  }

  /// `getByLabel` inside this iframe.
  #[must_use]
  pub fn get_by_label(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.locator(&build_text_like_selector("internal:label", text, opts), None)
  }

  /// `getByPlaceholder` inside this iframe.
  #[must_use]
  pub fn get_by_placeholder(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.locator(&build_attr_selector("placeholder", text, opts), None)
  }

  /// `getByAltText` inside this iframe.
  #[must_use]
  pub fn get_by_alt_text(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.locator(&build_attr_selector("alt", text, opts), None)
  }

  /// `getByTitle` inside this iframe.
  #[must_use]
  pub fn get_by_title(&self, text: &StringOrRegex, opts: &TextOptions) -> Locator {
    self.locator(&build_attr_selector("title", text, opts), None)
  }

  /// The locator pointing at the `<iframe>` element itself, in the
  /// parent frame's context. Mirrors Playwright's `frameLocator.owner()`.
  #[must_use]
  pub fn owner(&self) -> Locator {
    Locator::new(self.frame.clone(), self.frame_selector.clone())
  }

  /// `frameLocator` for a nested `<iframe>`. Mirrors Playwright's
  /// `frameLocator.frameLocator(selector)` — appends an enter-frame
  /// step plus the next iframe selector.
  #[must_use]
  pub fn frame_locator(&self, selector: &str) -> FrameLocator {
    FrameLocator {
      frame: self.frame.clone(),
      frame_selector: self.enter(selector),
    }
  }

  /// First matching iframe (`nth=0`).
  #[must_use]
  pub fn first(&self) -> FrameLocator {
    FrameLocator {
      frame: self.frame.clone(),
      frame_selector: format!("{} >> nth=0", self.frame_selector),
    }
  }

  /// Last matching iframe (`nth=-1`).
  #[must_use]
  pub fn last(&self) -> FrameLocator {
    FrameLocator {
      frame: self.frame.clone(),
      frame_selector: format!("{} >> nth=-1", self.frame_selector),
    }
  }

  /// Nth matching iframe.
  #[must_use]
  pub fn nth(&self, index: i32) -> FrameLocator {
    FrameLocator {
      frame: self.frame.clone(),
      frame_selector: format!("{} >> nth={index}", self.frame_selector),
    }
  }
}

impl std::fmt::Debug for FrameLocator {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("FrameLocator")
      .field("frame_selector", &self.frame_selector)
      .field("frame", &self.frame)
      .finish()
  }
}

// ── Selector builders ───────────────────────────────────────────────────────

/// Compute the drag press/release point from an element's bounding rect and
/// an optional `position`. When `position` is `Some`, the point is the
/// element's padding-box top-left offset by `(position.x, position.y)` —
/// matching Playwright's `sourcePosition` / `targetPosition` semantics. When
/// `position` is `None`, the element's center is used.
/// Page-side body for [`Locator::drop`]. Runs with `this` bound to the
/// target element and a JSON-literal `arg` ({payloads, data, modifiers,
/// position}) in scope. Mirrors Playwright's `dom.ts::_drop` page-side
/// closure: builds a `DataTransfer`, adds the `File` objects + `setData`
/// entries, then dispatches `dragenter` / `dragover` / `drop`. Returns a
/// status string the Rust side maps to `Ok`/`Err`.
const DROP_BODY: &str = r"
  if (!this.isConnected || this.nodeType !== 1) return 'error:notconnected';
  this.scrollIntoViewIfNeeded ? this.scrollIntoViewIfNeeded() : this.scrollIntoView();
  const r = this.getBoundingClientRect();
  const point = arg.position
    ? { x: r.x + arg.position.x, y: r.y + arg.position.y }
    : { x: r.x + r.width / 2, y: r.y + r.height / 2 };
  const dt = new DataTransfer();
  for (const p of arg.payloads) {
    const bin = atob(p.buffer);
    const bytes = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
    dt.items.add(new File([bytes], p.name, { type: p.mimeType }));
  }
  for (const entry of arg.data) dt.setData(entry.mimeType, entry.value);
  const makeEvent = (type) => new DragEvent(type, {
    bubbles: true,
    cancelable: true,
    composed: true,
    clientX: point.x,
    clientY: point.y,
    altKey: arg.modifiers.alt,
    ctrlKey: arg.modifiers.ctrl,
    metaKey: arg.modifiers.meta,
    shiftKey: arg.modifiers.shift,
    dataTransfer: dt,
  });
  this.dispatchEvent(makeEvent('dragenter'));
  const over = makeEvent('dragover');
  this.dispatchEvent(over);
  if (!over.defaultPrevented) {
    this.dispatchEvent(makeEvent('dragleave'));
    return 'not-accepted';
  }
  this.dispatchEvent(makeEvent('drop'));
  return 'accepted';
";

/// Lower a [`crate::options::DropPayload`]'s files into the page-side
/// `{name, mimeType, buffer(base64)}` records that [`DROP_BODY`] rebuilds
/// into `File` objects. Disk paths are read into buffers (matching
/// Playwright's co-located server path); in-memory payloads carry their
/// bytes already.
fn lower_drop_files(files: Option<crate::options::InputFiles>) -> Result<Vec<serde_json::Value>> {
  use base64::Engine as _;
  match files {
    Some(crate::options::InputFiles::Paths(paths)) => {
      let mut out = Vec::with_capacity(paths.len());
      for p in paths {
        let bytes = std::fs::read(&p)
          .map_err(|e| crate::error::FerriError::Backend(format!("failed to read drop file {}: {e}", p.display())))?;
        let name = p
          .file_name()
          .map(|n| n.to_string_lossy().into_owned())
          .unwrap_or_default();
        out.push(serde_json::json!({
          "name": name,
          "mimeType": guess_mime_type(&name),
          "buffer": base64::engine::general_purpose::STANDARD.encode(&bytes),
        }));
      }
      Ok(out)
    },
    Some(crate::options::InputFiles::Payloads(payloads)) => Ok(
      payloads
        .into_iter()
        .map(|p| {
          let mime = if p.mime_type.is_empty() {
            "application/octet-stream".to_string()
          } else {
            p.mime_type
          };
          serde_json::json!({
            "name": p.name,
            "mimeType": mime,
            "buffer": base64::engine::general_purpose::STANDARD.encode(&p.buffer),
          })
        })
        .collect(),
    ),
    None => Ok(Vec::new()),
  }
}

/// Best-effort MIME type for a filename based on its extension. Used by
/// [`Locator::drop`] when reading `DropPayload` file paths from disk, where
/// the caller did not supply an explicit MIME type. Falls back to
/// `application/octet-stream` for unknown extensions.
fn guess_mime_type(name: &str) -> &'static str {
  let ext = name.rsplit('.').next().unwrap_or("").to_ascii_lowercase();
  match ext.as_str() {
    "txt" | "text" => "text/plain",
    "html" | "htm" => "text/html",
    "css" => "text/css",
    "csv" => "text/csv",
    "js" | "mjs" => "text/javascript",
    "json" => "application/json",
    "xml" => "application/xml",
    "pdf" => "application/pdf",
    "png" => "image/png",
    "jpg" | "jpeg" => "image/jpeg",
    "gif" => "image/gif",
    "svg" => "image/svg+xml",
    "webp" => "image/webp",
    "zip" => "application/zip",
    _ => "application/octet-stream",
  }
}

fn rect_point(rect: &serde_json::Value, position: Option<crate::options::Point>) -> (f64, f64) {
  let x = rect.get("x").and_then(serde_json::Value::as_f64).unwrap_or(0.0);
  let y = rect.get("y").and_then(serde_json::Value::as_f64).unwrap_or(0.0);
  let width = rect.get("width").and_then(serde_json::Value::as_f64).unwrap_or(0.0);
  let height = rect.get("height").and_then(serde_json::Value::as_f64).unwrap_or(0.0);
  match position {
    Some(p) => (x + p.x, y + p.y),
    None => (x + width / 2.0, y + height / 2.0),
  }
}

pub(crate) fn build_role_selector(role: &str, opts: &RoleOptions) -> String {
  let mut sel = format!("internal:role={role}");
  if let Some(name) = &opts.name {
    let escaped = escape_for_attribute_selector(name, opts.exact == Some(true));
    let _ = write!(sel, "[name={escaped}]");
  }
  if let Some(checked) = opts.checked {
    let _ = write!(sel, "[checked={checked}]");
  }
  if let Some(disabled) = opts.disabled {
    let _ = write!(sel, "[disabled={disabled}]");
  }
  if let Some(expanded) = opts.expanded {
    let _ = write!(sel, "[expanded={expanded}]");
  }
  if let Some(level) = opts.level {
    let _ = write!(sel, "[level={level}]");
  }
  if let Some(pressed) = opts.pressed {
    let _ = write!(sel, "[pressed={pressed}]");
  }
  if let Some(selected) = opts.selected {
    let _ = write!(sel, "[selected={selected}]");
  }
  if let Some(include_hidden) = opts.include_hidden {
    let _ = write!(sel, "[include-hidden={include_hidden}]");
  }
  sel
}

/// Build a Playwright-native text-engine selector body. `engine_prefix`
/// is one of `internal:text` / `internal:label`. For `text: String` we
/// emit `"quoted"i` / `"quoted"s`; for `text: Regex` we emit
/// `/source/flags`. Mirrors
/// `packages/isomorphic/stringUtils.ts::escapeForTextSelector` from
/// `/tmp/playwright`.
pub(crate) fn build_text_like_selector(engine_prefix: &str, text: &StringOrRegex, opts: &TextOptions) -> String {
  let body = escape_for_text_selector(text, opts.exact == Some(true));
  format!("{engine_prefix}={body}")
}

/// Build a Playwright-native attribute-engine selector body of the
/// form `internal:attr=[<name>=<escaped>]` for `get_by_alt_text`,
/// `get_by_title`, `get_by_placeholder`. Mirrors
/// `packages/isomorphic/locatorUtils.ts::getByAttributeTextSelector`.
pub(crate) fn build_attr_selector(attr: &str, value: &StringOrRegex, opts: &TextOptions) -> String {
  let escaped = escape_for_attribute_selector(value, opts.exact == Some(true));
  format!("internal:attr=[{attr}={escaped}]")
}

/// Build a `get_by_test_id` selector. Testid matches are always exact
/// per Playwright.
pub(crate) fn build_testid_selector(attr_name: &str, testid: &StringOrRegex) -> String {
  let escaped = escape_for_attribute_selector(testid, true);
  format!("internal:testid=[{attr_name}={escaped}]")
}

/// Port of Playwright's `escapeForTextSelector` from
/// `/tmp/playwright/packages/isomorphic/stringUtils.ts`.
///
/// For a literal string returns `"quoted"i` (substring,
/// case-insensitive) or `"quoted"s` (exact, case-sensitive). For a
/// regex returns the `/source/flags` literal form with `>>` escaped so
/// the selector chain operator doesn't collide. `unicode`/`unicodeSets`
/// regex flags are preserved — the injected engine's `RegExp`
/// construction handles them natively.
fn escape_for_text_selector(value: &StringOrRegex, exact: bool) -> String {
  match value {
    StringOrRegex::String(s) => {
      let quoted = serde_json::to_string(s).unwrap_or_else(|_| format!("\"{s}\""));
      format!("{quoted}{}", if exact { "s" } else { "i" })
    },
    StringOrRegex::Regex { source, flags } => escape_regex_for_selector(source, flags),
  }
}

/// Port of Playwright's `escapeForAttributeSelector`.
fn escape_for_attribute_selector(value: &StringOrRegex, exact: bool) -> String {
  match value {
    StringOrRegex::String(s) => {
      let escaped = s.replace('\\', "\\\\").replace('"', "\\\"");
      format!("\"{escaped}\"{}", if exact { "s" } else { "i" })
    },
    StringOrRegex::Regex { source, flags } => escape_regex_for_selector(source, flags),
  }
}

/// Port of Playwright's `escapeRegexForSelector`. For regexes with
/// `unicode` / `unicodeSets` flags we emit the source verbatim (per the
/// upstream comment — identity character escapes aren't allowed in
/// those modes). Otherwise we escape `>>` so the selector chain
/// operator doesn't collide with the regex literal.
fn escape_regex_for_selector(source: &str, flags: &str) -> String {
  let has_unicode = flags.contains('u') || flags.contains('v');
  if has_unicode {
    format!("/{source}/{flags}")
  } else {
    // Escape `>>` as `\>\>` so the selector chain operator can't
    // prematurely split the regex literal. Matches Playwright's
    // `String(re).replace(/>>/g, '\\>\\>')` step — quote escaping is
    // only needed for attribute selectors, which our callers handle
    // separately.
    let escaped_source = source.replace(">>", "\\>\\>");
    format!("/{escaped_source}/{flags}")
  }
}

/// Produce the Playwright-compatible JSON-quoted form of an inner
/// selector string — matches the output of `JSON.stringify(str)` in JS.
/// Used by [`Locator::filter`] and [`Locator::and`] / [`Locator::or`]
/// when embedding nested selector text in `internal:*` clauses.
///
/// Falls back to a Rust `{:?}` debug form if `serde_json` cannot encode
/// (impossible for valid UTF-8 strings, but kept for defensive symmetry
/// with existing call sites at `and` / `or`).
fn json_quote(s: &str) -> String {
  serde_json::to_string(s).unwrap_or_else(|_| format!("{s:?}"))
}

// ── ariaSnapshot cross-iframe stitching ─────────────────────────────────────
//
// Mirrors Playwright's server `ariaSnapshotForFrame` /
// `ariaSnapshotFrameRef`
// (`/tmp/playwright/packages/playwright-core/src/server/page.ts:1103`).
// The injected `incrementalAriaSnapshot` renders one frame and reports
// the `<iframe>` nodes it gave refs to (`iframeRefs` / `iframeDepths`);
// the host resolves each into its child browsing context and splices
// the child render beneath the parent's `- iframe [ref=...]` line.

/// Decoded result of `window.__fd.incrementalAriaSnapshot`.
#[derive(serde::Deserialize, Default)]
struct AriaRaw {
  #[serde(default)]
  full: String,
  /// May contain `null` entries — `mode: 'default'` assigns no refs, so
  /// the renderer pushes `undefined` for un-reffed iframes (filtered out
  /// exactly like Playwright's `ref in iframeDepths` check).
  #[serde(default, rename = "iframeRefs")]
  iframe_refs: Vec<Option<String>>,
  #[serde(default, rename = "iframeDepths")]
  iframe_depths: std::collections::HashMap<String, i32>,
}

/// Build the `AriaTreeOptions` JSON for the injected call. `refPrefix`
/// is omitted when empty (top frame) — the vendored injected treats
/// missing and `''` identically (`options.refPrefix ?? ''`).
fn aria_opts_json(mode: &str, depth: Option<i32>, ref_prefix: &str) -> String {
  let mut m = serde_json::Map::new();
  m.insert("mode".into(), serde_json::Value::String(mode.to_string()));
  if let Some(d) = depth {
    m.insert("depth".into(), serde_json::Value::Number(d.into()));
  }
  if !ref_prefix.is_empty() {
    m.insert("refPrefix".into(), serde_json::Value::String(ref_prefix.to_string()));
  }
  serde_json::Value::Object(m).to_string()
}

fn parse_aria_raw(s: &str) -> Result<AriaRaw> {
  if s.trim().is_empty() {
    return Ok(AriaRaw::default());
  }
  serde_json::from_str(s).map_err(|e| crate::error::FerriError::evaluation(format!("ariaSnapshot parse: {e}")))
}

/// Render `frame` (already snapshotted into `raw`), then recurse into
/// every rendered child iframe and splice the child lines under the
/// matching `- iframe [ref=...]` line. Boxed so the
/// frame -> child-frame recursion has a finite future size.
fn aria_stitch_frame(
  frame: crate::frame::Frame,
  raw: AriaRaw,
  mode: String,
  depth: Option<i32>,
  seq: Arc<std::sync::atomic::AtomicU32>,
) -> futures::future::BoxFuture<'static, Result<Vec<String>>> {
  Box::pin(async move {
    // Playwright: `iframeRefs.filter(ref => ref in iframeDepths)` —
    // preserves order so `indexOf(ref)` aligns with `childSnapshots`.
    let rendered: Vec<String> = raw
      .iframe_refs
      .iter()
      .flatten()
      .filter(|r| raw.iframe_depths.contains_key(*r))
      .cloned()
      .collect();

    let mut child_snaps: Vec<Vec<String>> = Vec::with_capacity(rendered.len());
    for refv in &rendered {
      child_snaps.push(aria_child_snapshot(&frame, refv, &mode, depth, &raw.iframe_depths, &seq).await?);
    }

    let re = regex::Regex::new(r"^(\s*)- iframe (?:\[active\] )?\[ref=([^\]]*)\]")
      .map_err(|e| crate::error::FerriError::evaluation(format!("ariaSnapshot iframe regex: {e}")))?;

    let mut out: Vec<String> = Vec::new();
    for line in raw.full.split('\n') {
      let Some(caps) = re.captures(line) else {
        out.push(line.to_string());
        continue;
      };
      let leading = caps.get(1).map_or("", |m| m.as_str());
      let refv = caps.get(2).map_or("", |m| m.as_str());
      let child = rendered.iter().position(|x| x == refv).map(|i| &child_snaps[i]);
      let has = child.is_some_and(|c| !c.is_empty());
      out.push(if has { format!("{line}:") } else { line.to_string() });
      if let Some(child_lines) = child {
        for l in child_lines {
          out.push(format!("{leading}  {l}"));
        }
      }
    }
    Ok(out)
  })
}

/// Resolve one child iframe (`refv`) into its content frame, snapshot
/// its `<body>`, and recurse. Mirrors `ariaSnapshotFrameRef`. A missing
/// element / unresolvable content frame yields an empty render (the
/// parent then keeps the bare `- iframe [ref=...]` line — Playwright
/// parity).
async fn aria_child_snapshot(
  frame: &crate::frame::Frame,
  refv: &str,
  mode: &str,
  depth: Option<i32>,
  depths: &std::collections::HashMap<String, i32>,
  seq: &Arc<std::sync::atomic::AtomicU32>,
) -> Result<Vec<String>> {
  // Tag the iframe in JS, then re-resolve it through the normal
  // selector + content-frame path (the same one `frameLocator` uses).
  // Passing a utility-eval JSHandle straight into `content_frame()`
  // breaks on BiDi ("no such handle" — the cross-context handle is not
  // valid for the contentWindow call); a freshly queried element is.
  const ARIA_FRAME_ATTR: &str = "data-fd-aria-ref";
  let ref_json = serde_json::to_string(refv).unwrap_or_else(|_| format!("{refv:?}"));
  let mark_js = format!("() => window.__fd.markIframeByAriaRef({ref_json}, \"{ARIA_FRAME_ATTR}\")");
  let marked = matches!(
    frame
      .evaluate(&mark_js, crate::protocol::SerializedArgument::default(), Some(true))
      .await?,
    crate::protocol::SerializedValue::Bool(true)
  );
  if !marked {
    return Ok(Vec::new());
  }
  let page = frame.page_arc();
  let frame_id: Option<&str> = if frame.is_main_frame() { None } else { Some(frame.id()) };
  let sel = format!("[{ARIA_FRAME_ATTR}=\"{refv}\"]");
  let Ok(iframe_node) = selectors::query_one(page.inner(), &sel, false, frame_id).await else {
    return Ok(Vec::new());
  };
  let iframe_handle = crate::element_handle::ElementHandle::from_any_element(Arc::clone(page), iframe_node).await?;
  let Some(child_frame) = iframe_handle.content_frame().await? else {
    return Ok(Vec::new());
  };

  // Playwright: `childDepth = options.depth ? depth - iframeDepth - 1
  // : undefined` — `0` is falsy there too, so it maps to "unlimited".
  let iframe_depth = depths.get(refv).copied().unwrap_or(0);
  let child_depth = match depth {
    Some(d) if d != 0 => Some(d - iframe_depth - 1),
    _ => None,
  };

  // Unique per-frame ref prefix so `[ref=...]` never collides across
  // frames (Playwright uses `frame.seq`; we just need uniqueness).
  let n = seq.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
  let prefix = format!("f{n}");
  let copts = aria_opts_json(mode, child_depth, &prefix);
  let body_js = format!("() => JSON.stringify(window.__fd.incrementalAriaSnapshot(document.body, {copts}))");
  let raw_s = child_frame
    .evaluate(&body_js, crate::protocol::SerializedArgument::default(), Some(true))
    .await?
    .as_str()
    .map(std::string::ToString::to_string)
    .unwrap_or_default();
  if raw_s.is_empty() {
    return Ok(Vec::new());
  }
  let child_raw = parse_aria_raw(&raw_s)?;
  aria_stitch_frame(child_frame, child_raw, mode.to_string(), child_depth, Arc::clone(seq)).await
}