-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaywright-example.js
More file actions
80 lines (67 loc) · 2.28 KB
/
playwright-example.js
File metadata and controls
80 lines (67 loc) · 2.28 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 Playwright Integration
*
* This example demonstrates how to use the Playwright class for browser automation
* including page navigation and extended page methods
*/
import { Playwright, createPlaywrightCDPSession, log as Log, sleep } from '@scrapeless-ai/sdk';
const logger = Log.withPrefix('playwright-example');
async function runExample() {
logger.debug('Starting browser...');
// Launch browser instance
const browser = await Playwright.connect({
sessionName: 'sdk-playwright-example',
sessionTTL: 180,
proxyCountry: 'US',
// proxyURL: '',
sessionRecording: true,
viewport: null
});
try {
const context = browser.contexts()[0];
logger.debug('Creating new page...');
// Create a new page
const page = await context.newPage();
const cdpSession = await createPlaywrightCDPSession(context, page);
await cdpSession.disableCaptchaAutoSolve();
// Navigate to target website
logger.debug('Navigating to target website...');
await page.goto('https://prenotami.esteri.it/');
// Wait for page to load
await page.waitForLoadState('networkidle');
const { error, liveURL } = await page.liveURL();
if (error) {
logger.error('Failed to get current page URL:', error);
} else {
logger.info('Current page URL:', liveURL);
}
const email = 'xxx.mel@yqdfw.org';
const password = 'xxx*';
await page.waitForSelector('#login-email');
await page.waitForSelector('#login-password');
await sleep(10_000);
await cdpSession.realFill('#login-email', email);
await cdpSession.realFill('#login-password', password);
// Wait to observe the result
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);