@@ -52,6 +52,16 @@ export interface MoveOptions extends BoxOptions, Pick<PathOptions, 'moveSpeed'>
52
52
* @default 500
53
53
*/
54
54
readonly overshootThreshold ?: number
55
+ /**
56
+ * Scroll behavior when target element is outside the visible window.
57
+ * @default undefined (default)
58
+ */
59
+ readonly scrollBehavior ?: ScrollBehavior
60
+ /**
61
+ * Time to wait after scrolling (when scrolling occurs due to target element being outside the visible window)
62
+ * @default 2000
63
+ */
64
+ readonly scrollWait ?: number
55
65
}
56
66
57
67
export interface ClickOptions extends MoveOptions {
@@ -66,7 +76,7 @@ export interface ClickOptions extends MoveOptions {
66
76
*/
67
77
readonly waitForClick ?: number
68
78
/**
69
- * @default 2000
79
+ * @default 200
70
80
*/
71
81
readonly moveDelay ?: number
72
82
}
@@ -472,6 +482,7 @@ export const createCursor = (
472
482
maxTries : 10 ,
473
483
overshootThreshold : 500 ,
474
484
randomizeMoveDelay : true ,
485
+ scrollWait : 200 ,
475
486
...defaultOptions ?. move ,
476
487
...options
477
488
} satisfies MoveOptions
@@ -516,17 +527,28 @@ export const createCursor = (
516
527
// Make sure the object is in view
517
528
const objectId = elem . remoteObject ( ) . objectId
518
529
if ( objectId !== undefined ) {
519
- try {
520
- await getCDPClient ( page ) . send ( 'DOM.scrollIntoViewIfNeeded' , {
521
- objectId
522
- } )
523
- } catch ( e ) {
530
+ const scrollElemIntoView = async ( ) : Promise < void > => await elem . evaluate ( ( e , scrollBehavior ) => e . scrollIntoView ( {
531
+ block : 'center' ,
532
+ behavior : scrollBehavior
533
+ } ) , optionsResolved . scrollBehavior )
534
+
535
+ if ( optionsResolved . scrollBehavior !== undefined ) {
536
+ await scrollElemIntoView ( )
537
+ } else {
538
+ try {
539
+ await getCDPClient ( page ) . send ( 'DOM.scrollIntoViewIfNeeded' , {
540
+ objectId
541
+ } )
542
+ } catch ( e ) {
524
543
// use regular JS scroll method as a fallback
525
- log ( 'Falling back to JS scroll method' , e )
526
- await elem . evaluate ( ( e ) => e . scrollIntoView ( { block : 'center' } ) )
527
- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) // Wait a bit until the scroll has finished
544
+ log ( 'Falling back to JS scroll method' , e )
545
+ await scrollElemIntoView ( )
546
+ }
528
547
}
548
+
549
+ await delay ( optionsResolved . scrollWait )
529
550
}
551
+
530
552
const box = await boundingBoxWithFallback ( page , elem )
531
553
const { height, width } = box
532
554
const destination = getRandomBoxPoint ( box , optionsResolved )
0 commit comments