-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpuppeteer-cdp-example.js
More file actions
94 lines (80 loc) · 2.93 KB
/
Copy pathpuppeteer-cdp-example.js
File metadata and controls
94 lines (80 loc) · 2.93 KB
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
/**
* Example: Puppeteer CDP Session with Extended Methods
* Filename: puppeteer-cdp-example.js
*
* Environment variables required:
* - SCRAPELESS_API_KEY: Your Scrapeless API key
*
* This example demonstrates how to use the extended CDP session methods
* for browser automation with Scrapeless API.
*/
import { Puppeteer, createPuppeteerCDPSession } from '../dist/index.js';
async function runExample() {
console.log('Creating Puppeteer browser instance...');
const browser = await Puppeteer.connect({
sessionName: 'cdp-example-session',
sessionTTL: 300,
proxyCountry: 'US'
});
try {
console.log('Creating new page...');
const page = await browser.newPage();
console.log('Creating Scrapeless-enhanced CDP session...');
const cdpSession = await createPuppeteerCDPSession(page);
console.log('Navigating to login page...');
await page.goto('https://app.scrapeless.com/login');
console.log('Getting current page URL using CDP...');
const urlResponse = await cdpSession.liveURL();
if (urlResponse.error) {
console.error('Error getting URL:', urlResponse.error);
} else {
console.log('Current URL:', urlResponse.liveURL);
}
console.log('Setting up captcha auto-solve...');
await cdpSession.setAutoSolve({
autoSolve: true,
options: [
{ type: 'recaptcha', disabled: false },
{ type: 'hcaptcha', disabled: false }
]
});
console.log('Filling login form using CDP methods...');
await cdpSession.realFill('#login-email', 'user@example.com');
await cdpSession.realFill('#login-password', 'password123');
console.log('Clicking submit button...');
await cdpSession.realClick('button[type="submit"]');
console.log('Waiting for potential captcha detection...');
const captchaResult = await cdpSession.waitCaptchaDetected({ timeout: 10000 });
if (captchaResult.success) {
console.log('Captcha detected:', captchaResult.type);
console.log('Solving captcha...');
const solveResult = await cdpSession.solveCaptcha({ timeout: 30000 });
if (solveResult.success) {
console.log('Captcha solved successfully!');
} else {
console.log('Failed to solve captcha:', solveResult.message);
}
} else {
console.log('No captcha detected or timeout reached');
}
console.log('Getting final URL...');
const finalUrlResponse = await cdpSession.liveURL();
if (!finalUrlResponse.error) {
console.log('Final URL:', finalUrlResponse.liveURL);
}
console.log('Example completed successfully!');
} catch (error) {
console.error('Error during example execution:', error.message);
} finally {
try {
if (browser) {
console.log('Closing browser...');
await browser.close();
}
} catch (cleanupError) {
console.error('Error during cleanup:', cleanupError.message);
}
}
}
// Run the example
runExample().catch(console.error);