-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpuppeteer-example.js
More file actions
80 lines (69 loc) · 2.72 KB
/
Copy pathpuppeteer-example.js
File metadata and controls
80 lines (69 loc) · 2.72 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
/**
* Example: Using Scrapeless SDK's Puppeteer Integration
*
* This example demonstrates how to use the Puppeteer class for browser automation
* including page navigation and extended CDP session methods
*/
import { Puppeteer, createPuppeteerCDPSession, log as Log, sleep } from '../dist/index.js';
const logger = Log.withPrefix('puppeteer-example');
async function runExample() {
logger.debug('Starting browser...');
// Launch browser instance
const browser = await Puppeteer.connect({
sessionName: 'sdk-puppeteer-example',
sessionTTL: 180,
proxyCountry: 'US',
sessionRecording: true,
defaultViewport: null
});
try {
logger.debug('Creating new page...');
// Create a new page
const page = await browser.newPage();
logger.debug('Creating CDP session with extended methods...');
// Navigate to target website
logger.debug('Navigating to target website...');
// if cookies.json exists, read it and set cookies
// if (fs.existsSync('./cookies.json')) {
// const cookies = JSON.parse(fs.readFileSync('./cookies.json', 'utf8'));
// // filter out invalid cookies
// const validCookies = cookies.filter(cookie => cookie.domain && cookie.name && cookie.value);
// await browser.setCookie(...validCookies);
// } else {
// await page.goto('https://www.google.com/', { waitUntil: 'networkidle0' });
// const cookies = await browser.cookies();
// fs.writeFileSync('./cookies.json', JSON.stringify(cookies, null, 2));
// }
await page.goto('https://www.google.com/', { waitUntil: 'networkidle0' });
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.disableCaptchaAutoSolve();
await page.goto('https://prenotami.esteri.it/');
// await page.goto('https://patrickhlauke.github.io/recaptcha/', { waitUntil: 'domcontentloaded' });
const email = '19374294169.mel@yqdfw.org';
const password = 'niuniuZHOU1986*';
await sleep(10_000);
logger.debug('Filling form using CDP methods...');
await cdpSession.realFill('#login-email', email);
await cdpSession.realFill('#login-password', password);
await sleep(5_000);
logger.debug('Solving captcha...');
const captcha = await cdpSession.solveCaptcha();
if (captcha.success) {
logger.info('Captcha detected:', captcha);
} else {
logger.error('Failed to detect captcha:', captcha.message);
return;
}
// await cdpSession.realClick('button[type="submit"]');
await sleep(10_000);
} catch (error) {
console.error('Error running example:', error);
} finally {
if (browser) {
logger.debug('Closing browser...');
await browser.close();
}
}
}
// Run the example
runExample().catch(console.error);