Skip to content

Commit 629daf8

Browse files
Yash-HirgudeB4nan
andauthored
feat: make handleCloudflareChallenge more configurable (#3247)
This PR enhances the Cloudflare challenge handling logic by making the click behaviour more flexible and reliable. Two new options are introduced in postNavigationHooks.ts: - `clickPositionCallback` Allows users to manually specify the exact coordinates (x, y) where the click should occur. This is useful when the default DOM-based position detection fails, or when the challenge layout varies across sites. - `preChallengeSleepSecs` Optional delay (in milliseconds) inserted before the first click attempt. This helps ensure the Cloudflare button and related elements are fully rendered before detection or clicking begins. Closes #3127 --------- Co-authored-by: Martin Adámek <banan23@gmail.com>
1 parent a8177d3 commit 629daf8

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

packages/playwright-crawler/src/internals/utils/playwright-utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@ interface HandleCloudflareChallengeOptions {
697697
isChallengeCallback?: (page: Page) => Promise<boolean>;
698698
/** Allows overriding the detection of Cloudflare "blocked page". */
699699
isBlockedCallback?: (page: Page) => Promise<boolean>;
700+
/** Allows overriding how the checkbox click position is calculated. */
701+
clickPositionCallback?: (page: Page) => Promise<{ x: number; y: number } | null>;
702+
/** Optional delay (in seconds) before the first click attempt on the challenge checkbox. Defaults to 1s. */
703+
preChallengeSleepSecs?: number;
700704
}
701705

702706
/**
@@ -788,18 +792,26 @@ async function handleCloudflareChallenge(
788792
return Math.round(100 * range * Math.random()) / 100;
789793
};
790794

791-
const x = bb.x + 30;
792-
const y = bb.y + 25;
795+
let x = bb.x + 30;
796+
let y = bb.y + 25;
793797

794798
// try to click the checkbox every second
795799
for (let i = 0; i < 10; i++) {
796-
await sleep(1000);
800+
await sleep((options.preChallengeSleepSecs ?? 1) * 1000);
797801

798802
// break early if we are no longer on the CF challenge page
799803
if (!(await isChallenge())) {
800804
break;
801805
}
802806

807+
if (options.clickPositionCallback) {
808+
const pos = await options.clickPositionCallback(page);
809+
if (pos) {
810+
x = pos.x;
811+
y = pos.y;
812+
}
813+
}
814+
803815
if (options.clickCallback) {
804816
await options.clickCallback(page, { x, y });
805817
continue;

0 commit comments

Comments
 (0)