@crawlee/playwright 4.0.0-beta.9 → 4.0.0-beta.91
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -13
- package/index.d.ts +1 -2
- package/index.js +0 -1
- package/internals/adaptive-playwright-crawler.d.ts +117 -63
- package/internals/adaptive-playwright-crawler.js +354 -214
- package/internals/enqueue-links/click-elements.d.ts +32 -14
- package/internals/enqueue-links/click-elements.js +57 -25
- package/internals/playwright-crawler.d.ts +104 -83
- package/internals/playwright-crawler.js +85 -41
- package/internals/playwright-launcher.d.ts +6 -5
- package/internals/playwright-launcher.js +10 -11
- package/internals/utils/playwright-utils.d.ts +58 -21
- package/internals/utils/playwright-utils.js +99 -86
- package/internals/utils/rendering-type-prediction.d.ts +27 -12
- package/internals/utils/rendering-type-prediction.js +67 -26
- package/package.json +16 -11
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/internals/adaptive-playwright-crawler.d.ts.map +0 -1
- package/internals/adaptive-playwright-crawler.js.map +0 -1
- package/internals/enqueue-links/click-elements.d.ts.map +0 -1
- package/internals/enqueue-links/click-elements.js.map +0 -1
- package/internals/playwright-crawler.d.ts.map +0 -1
- package/internals/playwright-crawler.js.map +0 -1
- package/internals/playwright-launcher.d.ts.map +0 -1
- package/internals/playwright-launcher.js.map +0 -1
- package/internals/utils/playwright-utils.d.ts.map +0 -1
- package/internals/utils/playwright-utils.js.map +0 -1
- package/internals/utils/rendering-type-prediction.d.ts.map +0 -1
- package/internals/utils/rendering-type-prediction.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BrowserCrawler,
|
|
1
|
+
import { BrowserCrawler, RequestState, Router, serviceLocator } from '@crawlee/browser';
|
|
2
2
|
import ow from 'ow';
|
|
3
3
|
import { PlaywrightLauncher } from './playwright-launcher.js';
|
|
4
|
-
import { gotoExtended,
|
|
4
|
+
import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
|
|
5
5
|
/**
|
|
6
6
|
* Provides a simple framework for parallel crawling of web pages
|
|
7
7
|
* using headless Chromium, Firefox and Webkit browsers with [Playwright](https://github.com/microsoft/playwright).
|
|
@@ -13,13 +13,15 @@ import { gotoExtended, registerUtilsToContext } from './utils/playwright-utils.j
|
|
|
13
13
|
* If the target website doesn't need JavaScript, consider using {@link CheerioCrawler},
|
|
14
14
|
* which downloads the pages using raw HTTP requests and is about 10x faster.
|
|
15
15
|
*
|
|
16
|
-
* The source URLs are represented using {@link Request} objects that are fed from
|
|
17
|
-
* {@link
|
|
18
|
-
*
|
|
16
|
+
* The source URLs are represented using {@link Request} objects that are fed from the
|
|
17
|
+
* {@link IRequestManager|request manager} provided via the {@link PlaywrightCrawlerOptions.requestManager|`requestManager`}
|
|
18
|
+
* constructor option (a {@link RequestQueue} is itself a request manager). To read from a read-only source such
|
|
19
|
+
* as a {@link RequestList} while still being able to enqueue new requests, combine it with a queue into a
|
|
20
|
+
* {@link RequestManagerTandem} via {@link IRequestLoader.toTandem|`requestLoader.toTandem()`} and pass the
|
|
21
|
+
* result as `requestManager`.
|
|
19
22
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
|
|
23
|
+
* > The {@link PlaywrightCrawlerOptions.requestList|`requestList`} and {@link PlaywrightCrawlerOptions.requestQueue|`requestQueue`}
|
|
24
|
+
* > options are deprecated; they are still accepted and folded into a single `requestManager` for back-compat.
|
|
23
25
|
*
|
|
24
26
|
* The crawler finishes when there are no more {@link Request} objects to crawl.
|
|
25
27
|
*
|
|
@@ -28,9 +30,9 @@ import { gotoExtended, registerUtilsToContext } from './utils/playwright-utils.j
|
|
|
28
30
|
*
|
|
29
31
|
* New pages are only opened when there is enough free CPU and memory available,
|
|
30
32
|
* using the functionality provided by the {@link AutoscaledPool} class.
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* {@link
|
|
33
|
+
* Concurrency is tuned via the `minConcurrency`, `maxConcurrency` and `maxRequestsPerMinute` options of the
|
|
34
|
+
* `PlaywrightCrawler` constructor, or, for finer control, by injecting a pre-configured
|
|
35
|
+
* {@link ConcurrencySystem|`concurrencySystem`}.
|
|
34
36
|
*
|
|
35
37
|
* Note that the pool of Playwright instances is internally managed by the [BrowserPool](https://github.com/apify/browser-pool) class.
|
|
36
38
|
*
|
|
@@ -66,19 +68,19 @@ import { gotoExtended, registerUtilsToContext } from './utils/playwright-utils.j
|
|
|
66
68
|
* @category Crawlers
|
|
67
69
|
*/
|
|
68
70
|
export class PlaywrightCrawler extends BrowserCrawler {
|
|
69
|
-
options;
|
|
70
|
-
config;
|
|
71
71
|
static optionsShape = {
|
|
72
72
|
...BrowserCrawler.optionsShape,
|
|
73
73
|
browserPoolOptions: ow.optional.object,
|
|
74
74
|
launcher: ow.optional.object,
|
|
75
|
+
ignoreIframes: ow.optional.boolean,
|
|
76
|
+
ignoreShadowRoots: ow.optional.boolean,
|
|
75
77
|
};
|
|
76
78
|
/**
|
|
77
79
|
* All `PlaywrightCrawler` parameters are passed via an options object.
|
|
78
80
|
*/
|
|
79
|
-
constructor(options = {}
|
|
81
|
+
constructor(options = {}) {
|
|
80
82
|
ow(options, 'PlaywrightCrawlerOptions', ow.object.exactShape(PlaywrightCrawler.optionsShape));
|
|
81
|
-
const { launchContext = {}, headless, ...browserCrawlerOptions } = options;
|
|
83
|
+
const { launchContext = {}, headless, contextPipelineBuilder, ...browserCrawlerOptions } = options;
|
|
82
84
|
const browserPoolOptions = {
|
|
83
85
|
...options.browserPoolOptions,
|
|
84
86
|
};
|
|
@@ -95,45 +97,87 @@ export class PlaywrightCrawler extends BrowserCrawler {
|
|
|
95
97
|
launchContext.launchOptions ??= {};
|
|
96
98
|
launchContext.launchOptions.headless = headless;
|
|
97
99
|
}
|
|
98
|
-
const playwrightLauncher = new PlaywrightLauncher(launchContext,
|
|
100
|
+
const playwrightLauncher = new PlaywrightLauncher(launchContext, options.configuration);
|
|
99
101
|
browserPoolOptions.browserPlugins = [playwrightLauncher.createBrowserPlugin()];
|
|
100
|
-
super({
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
super({
|
|
103
|
+
...browserCrawlerOptions,
|
|
104
|
+
launchContext,
|
|
105
|
+
browserPoolOptions,
|
|
106
|
+
contextPipelineBuilder: contextPipelineBuilder ?? (() => this.buildContextPipeline()),
|
|
107
|
+
});
|
|
103
108
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
await super._runRequestHandler(context);
|
|
109
|
+
buildContextPipeline() {
|
|
110
|
+
return super.buildContextPipeline().compose({ action: this.enhanceContext.bind(this) });
|
|
107
111
|
}
|
|
108
112
|
async _navigationHandler(crawlingContext, gotoOptions) {
|
|
109
113
|
return gotoExtended(crawlingContext.page, crawlingContext.request, gotoOptions);
|
|
110
114
|
}
|
|
115
|
+
async enhanceContext(context) {
|
|
116
|
+
const waitForSelector = async (selector, timeoutMs = 5_000) => {
|
|
117
|
+
const locator = context.page.locator(selector).first();
|
|
118
|
+
await locator.waitFor({ timeout: timeoutMs, state: 'attached' });
|
|
119
|
+
};
|
|
120
|
+
const downloads = [];
|
|
121
|
+
context.page.on('download', (download) => downloads.push(download));
|
|
122
|
+
return {
|
|
123
|
+
injectFile: async (filePath, options) => playwrightUtils.injectFile(context.page, filePath, options),
|
|
124
|
+
injectJQuery: async () => {
|
|
125
|
+
if (context.request.state === RequestState.BEFORE_NAV) {
|
|
126
|
+
context.log.warning('Using injectJQuery() in preNavigationHooks leads to unstable results. Use it in a postNavigationHook or a requestHandler instead.');
|
|
127
|
+
await playwrightUtils.injectJQuery(context.page);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
await playwrightUtils.injectJQuery(context.page, { surviveNavigations: false });
|
|
131
|
+
},
|
|
132
|
+
blockRequests: async (options) => playwrightUtils.blockRequests(context.page, options),
|
|
133
|
+
waitForSelector,
|
|
134
|
+
parseWithCheerio: async (selector, timeoutMs = 5_000) => {
|
|
135
|
+
if (selector) {
|
|
136
|
+
await waitForSelector(selector, timeoutMs);
|
|
137
|
+
}
|
|
138
|
+
return playwrightUtils.parseWithCheerio(context.page, this.ignoreShadowRoots, this.ignoreIframes);
|
|
139
|
+
},
|
|
140
|
+
infiniteScroll: async (options) => playwrightUtils.infiniteScroll(context.page, options),
|
|
141
|
+
listDownloads: async () => downloads,
|
|
142
|
+
saveSnapshot: async (options) => playwrightUtils.saveSnapshot(context.page, {
|
|
143
|
+
...options,
|
|
144
|
+
configuration: serviceLocator.getConfiguration(),
|
|
145
|
+
}),
|
|
146
|
+
enqueueLinksByClickingElements: async (options) => playwrightUtils.enqueueLinksByClickingElements({
|
|
147
|
+
...options,
|
|
148
|
+
page: context.page,
|
|
149
|
+
requestManager: this.requestManager,
|
|
150
|
+
}),
|
|
151
|
+
compileScript: (scriptString, ctx) => playwrightUtils.compileScript(scriptString, ctx),
|
|
152
|
+
closeCookieModals: async () => playwrightUtils.closeCookieModals(context.page),
|
|
153
|
+
handleCloudflareChallenge: async (options) => {
|
|
154
|
+
return playwrightUtils.handleCloudflareChallenge(context.page, context.request.url, options);
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
111
158
|
}
|
|
112
159
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* Defaults to the {@link PlaywrightCrawlingContext}.
|
|
116
|
-
*
|
|
117
|
-
* > Serves as a shortcut for using `Router.create<PlaywrightCrawlingContext>()`.
|
|
160
|
+
* Returns a `postNavigationHooks`-ready hook that runs {@link PlaywrightContextUtils.handleCloudflareChallenge}
|
|
161
|
+
* and propagates the post-challenge {@link Response} back into the crawling context via its return value.
|
|
118
162
|
*
|
|
163
|
+
* **Example usage**
|
|
119
164
|
* ```ts
|
|
120
|
-
* import { PlaywrightCrawler,
|
|
121
|
-
*
|
|
122
|
-
* const router = createPlaywrightRouter();
|
|
123
|
-
* router.addHandler('label-a', async (ctx) => {
|
|
124
|
-
* ctx.log.info('...');
|
|
125
|
-
* });
|
|
126
|
-
* router.addDefaultHandler(async (ctx) => {
|
|
127
|
-
* ctx.log.info('...');
|
|
128
|
-
* });
|
|
165
|
+
* import { PlaywrightCrawler, handleCloudflareChallengeHook } from 'crawlee';
|
|
129
166
|
*
|
|
130
167
|
* const crawler = new PlaywrightCrawler({
|
|
131
|
-
*
|
|
168
|
+
* postNavigationHooks: [handleCloudflareChallengeHook()],
|
|
132
169
|
* });
|
|
133
|
-
* await crawler.run();
|
|
134
170
|
* ```
|
|
135
171
|
*/
|
|
136
|
-
export function
|
|
137
|
-
return
|
|
172
|
+
export function handleCloudflareChallengeHook(options) {
|
|
173
|
+
return async (context) => {
|
|
174
|
+
const response = await context.handleCloudflareChallenge(options);
|
|
175
|
+
if (response !== undefined) {
|
|
176
|
+
return { response };
|
|
177
|
+
}
|
|
178
|
+
return undefined;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
export function createPlaywrightRouter(routesOrSchemas) {
|
|
182
|
+
return Router.create(routesOrSchemas);
|
|
138
183
|
}
|
|
139
|
-
//# sourceMappingURL=playwright-crawler.js.map
|
|
@@ -70,7 +70,7 @@ export interface PlaywrightLaunchContext extends BrowserLaunchContext<LaunchOpti
|
|
|
70
70
|
* @ignore
|
|
71
71
|
*/
|
|
72
72
|
export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin> {
|
|
73
|
-
readonly
|
|
73
|
+
readonly configuration: Configuration;
|
|
74
74
|
protected static optionsShape: {
|
|
75
75
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
76
76
|
launcher: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
@@ -84,6 +84,8 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
84
84
|
useIncognitoPages: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
85
85
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
86
86
|
browserPerProxy: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
87
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
88
|
+
ignoreProxyCertificate: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
87
89
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
88
90
|
userDataDir: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
|
|
89
91
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
@@ -94,7 +96,7 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
94
96
|
/**
|
|
95
97
|
* All `PlaywrightLauncher` parameters are passed via this launchContext object.
|
|
96
98
|
*/
|
|
97
|
-
constructor(launchContext?: PlaywrightLaunchContext,
|
|
99
|
+
constructor(launchContext?: PlaywrightLaunchContext, configuration?: Configuration);
|
|
98
100
|
}
|
|
99
101
|
/**
|
|
100
102
|
* Launches headless browsers using Playwright pre-configured to work within the Apify platform.
|
|
@@ -125,9 +127,8 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
125
127
|
* Optional settings passed to `browserType.launch()`. In addition to
|
|
126
128
|
* [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
|
|
127
129
|
* the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
|
|
128
|
-
* @param [
|
|
130
|
+
* @param [configuration]
|
|
129
131
|
* @returns
|
|
130
132
|
* Promise that resolves to Playwright's `Browser` instance.
|
|
131
133
|
*/
|
|
132
|
-
export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext,
|
|
133
|
-
//# sourceMappingURL=playwright-launcher.d.ts.map
|
|
134
|
+
export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext, configuration?: Configuration): Promise<Browser>;
|
|
@@ -6,7 +6,7 @@ import ow from 'ow';
|
|
|
6
6
|
* @ignore
|
|
7
7
|
*/
|
|
8
8
|
export class PlaywrightLauncher extends BrowserLauncher {
|
|
9
|
-
|
|
9
|
+
configuration;
|
|
10
10
|
static optionsShape = {
|
|
11
11
|
...BrowserLauncher.optionsShape,
|
|
12
12
|
launcher: ow.optional.object,
|
|
@@ -15,7 +15,7 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
15
15
|
/**
|
|
16
16
|
* All `PlaywrightLauncher` parameters are passed via this launchContext object.
|
|
17
17
|
*/
|
|
18
|
-
constructor(launchContext = {},
|
|
18
|
+
constructor(launchContext = {}, configuration = Configuration.getGlobalConfiguration()) {
|
|
19
19
|
ow(launchContext, 'PlaywrightLauncherOptions', ow.object.exactShape(PlaywrightLauncher.optionsShape));
|
|
20
20
|
const { launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium, } = launchContext;
|
|
21
21
|
const { launchOptions = {}, ...rest } = launchContext;
|
|
@@ -23,11 +23,11 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
23
23
|
...rest,
|
|
24
24
|
launchOptions: {
|
|
25
25
|
...launchOptions,
|
|
26
|
-
executablePath: getDefaultExecutablePath(launchContext,
|
|
26
|
+
executablePath: getDefaultExecutablePath(launchContext, configuration),
|
|
27
27
|
},
|
|
28
28
|
launcher,
|
|
29
|
-
},
|
|
30
|
-
this.
|
|
29
|
+
}, configuration);
|
|
30
|
+
this.configuration = configuration;
|
|
31
31
|
this.Plugin = PlaywrightPlugin;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -36,8 +36,8 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
36
36
|
* @returns default path to browser.
|
|
37
37
|
* @ignore
|
|
38
38
|
*/
|
|
39
|
-
function getDefaultExecutablePath(launchContext,
|
|
40
|
-
const pathFromPlaywrightImage =
|
|
39
|
+
function getDefaultExecutablePath(launchContext, configuration) {
|
|
40
|
+
const pathFromPlaywrightImage = configuration.defaultBrowserPath;
|
|
41
41
|
const { launchOptions = {} } = launchContext;
|
|
42
42
|
if (launchOptions.executablePath) {
|
|
43
43
|
return launchOptions.executablePath;
|
|
@@ -79,12 +79,11 @@ function getDefaultExecutablePath(launchContext, config) {
|
|
|
79
79
|
* Optional settings passed to `browserType.launch()`. In addition to
|
|
80
80
|
* [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
|
|
81
81
|
* the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
|
|
82
|
-
* @param [
|
|
82
|
+
* @param [configuration]
|
|
83
83
|
* @returns
|
|
84
84
|
* Promise that resolves to Playwright's `Browser` instance.
|
|
85
85
|
*/
|
|
86
|
-
export async function launchPlaywright(launchContext,
|
|
87
|
-
const playwrightLauncher = new PlaywrightLauncher(launchContext,
|
|
86
|
+
export async function launchPlaywright(launchContext, configuration = Configuration.getGlobalConfiguration()) {
|
|
87
|
+
const playwrightLauncher = new PlaywrightLauncher(launchContext, configuration);
|
|
88
88
|
return playwrightLauncher.launch();
|
|
89
89
|
}
|
|
90
|
-
//# sourceMappingURL=playwright-launcher.js.map
|
|
@@ -17,14 +17,13 @@
|
|
|
17
17
|
* ```
|
|
18
18
|
* @module playwrightUtils
|
|
19
19
|
*/
|
|
20
|
-
import { Configuration, type Request
|
|
21
|
-
import type { BatchAddRequestsResult } from '@crawlee/types';
|
|
22
|
-
import { type CheerioRoot
|
|
20
|
+
import { Configuration, type Request } from '@crawlee/browser';
|
|
21
|
+
import type { BatchAddRequestsResult, Dictionary } from '@crawlee/types';
|
|
22
|
+
import { type CheerioRoot } from '@crawlee/utils';
|
|
23
23
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
24
|
-
import type { Page, Response } from 'playwright';
|
|
24
|
+
import type { Download, Page, Response } from 'playwright';
|
|
25
25
|
import type { EnqueueLinksByClickingElementsOptions } from '../enqueue-links/click-elements.js';
|
|
26
26
|
import { enqueueLinksByClickingElements } from '../enqueue-links/click-elements.js';
|
|
27
|
-
import type { PlaywrightCrawlerOptions, PlaywrightCrawlingContext } from '../playwright-crawler.js';
|
|
28
27
|
import { RenderingTypePredictor } from './rendering-type-prediction.js';
|
|
29
28
|
export interface InjectFileOptions {
|
|
30
29
|
/**
|
|
@@ -266,9 +265,9 @@ export interface SaveSnapshotOptions {
|
|
|
266
265
|
keyValueStoreName?: string | null;
|
|
267
266
|
/**
|
|
268
267
|
* Configuration of the crawler that will be used to save the snapshot.
|
|
269
|
-
* @default Configuration.
|
|
268
|
+
* @default Configuration.getGlobalConfiguration()
|
|
270
269
|
*/
|
|
271
|
-
|
|
270
|
+
configuration?: Configuration;
|
|
272
271
|
}
|
|
273
272
|
/**
|
|
274
273
|
* Saves a full screenshot and HTML of the current page into a Key-Value store.
|
|
@@ -290,7 +289,7 @@ export declare function saveSnapshot(page: Page, options?: SaveSnapshotOptions):
|
|
|
290
289
|
*/
|
|
291
290
|
export declare function parseWithCheerio(page: Page, ignoreShadowRoots?: boolean, ignoreIframes?: boolean): Promise<CheerioRoot>;
|
|
292
291
|
export declare function closeCookieModals(page: Page): Promise<void>;
|
|
293
|
-
interface HandleCloudflareChallengeOptions {
|
|
292
|
+
export interface HandleCloudflareChallengeOptions {
|
|
294
293
|
/** Logging defaults to the `debug` level, use this flag to log to `info` level instead. */
|
|
295
294
|
verbose?: boolean;
|
|
296
295
|
/** How long should we wait after the challenge is completed for the final page to load. */
|
|
@@ -304,6 +303,13 @@ interface HandleCloudflareChallengeOptions {
|
|
|
304
303
|
isChallengeCallback?: (page: Page) => Promise<boolean>;
|
|
305
304
|
/** Allows overriding the detection of Cloudflare "blocked page". */
|
|
306
305
|
isBlockedCallback?: (page: Page) => Promise<boolean>;
|
|
306
|
+
/** Allows overriding how the checkbox click position is calculated. */
|
|
307
|
+
clickPositionCallback?: (page: Page) => Promise<{
|
|
308
|
+
x: number;
|
|
309
|
+
y: number;
|
|
310
|
+
} | null>;
|
|
311
|
+
/** Optional delay (in seconds) before the first click attempt on the challenge checkbox. Defaults to 1s. */
|
|
312
|
+
preChallengeSleepSecs?: number;
|
|
307
313
|
}
|
|
308
314
|
/**
|
|
309
315
|
* This helper tries to solve the Cloudflare challenge automatically by clicking on the checkbox.
|
|
@@ -312,23 +318,24 @@ interface HandleCloudflareChallengeOptions {
|
|
|
312
318
|
* result in a SessionError which will be automatically retried, so only successful requests will get
|
|
313
319
|
* into the `requestHandler`.
|
|
314
320
|
*
|
|
321
|
+
* On a successfully solved challenge the page is reloaded and the new {@link Response} is returned, so
|
|
322
|
+
* it can be propagated back to the crawling context via a hook return value (see
|
|
323
|
+
* {@link handleCloudflareChallengeHook}).
|
|
324
|
+
*
|
|
315
325
|
* Works best with camoufox.
|
|
316
326
|
*
|
|
317
327
|
* **Example usage**
|
|
318
328
|
* ```ts
|
|
319
329
|
* postNavigationHooks: [
|
|
320
|
-
* async ({ handleCloudflareChallenge })
|
|
321
|
-
* await handleCloudflareChallenge();
|
|
322
|
-
* },
|
|
330
|
+
* async (context) => ({ response: await context.handleCloudflareChallenge() }),
|
|
323
331
|
* ],
|
|
324
332
|
* ```
|
|
325
333
|
*
|
|
326
334
|
* @param page Playwright [`Page`](https://playwright.dev/docs/api/class-page) object
|
|
327
335
|
* @param url current URL for request identification, only used for logging
|
|
328
|
-
* @param [session] current session object
|
|
329
336
|
* @param [options]
|
|
330
337
|
*/
|
|
331
|
-
declare function handleCloudflareChallenge(page: Page, url: string,
|
|
338
|
+
declare function handleCloudflareChallenge(page: Page, url: string, options?: HandleCloudflareChallengeOptions): Promise<Response | undefined>;
|
|
332
339
|
/** @internal */
|
|
333
340
|
export interface PlaywrightContextUtils {
|
|
334
341
|
/**
|
|
@@ -480,7 +487,7 @@ export interface PlaywrightContextUtils {
|
|
|
480
487
|
*
|
|
481
488
|
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
482
489
|
*/
|
|
483
|
-
enqueueLinksByClickingElements(options: Omit<EnqueueLinksByClickingElementsOptions, 'page' | '
|
|
490
|
+
enqueueLinksByClickingElements(options: Omit<EnqueueLinksByClickingElementsOptions, 'page' | 'requestManager'>): Promise<BatchAddRequestsResult>;
|
|
484
491
|
/**
|
|
485
492
|
* Compiles a Playwright script into an async function that may be executed at any time
|
|
486
493
|
* by providing it with the following object:
|
|
@@ -510,6 +517,15 @@ export interface PlaywrightContextUtils {
|
|
|
510
517
|
compileScript(scriptString: string, ctx?: Dictionary): CompiledScriptFunction;
|
|
511
518
|
/**
|
|
512
519
|
* Tries to close cookie consent modals on the page. Based on the I Don't Care About Cookies browser extension.
|
|
520
|
+
*
|
|
521
|
+
* Note that this method requires the idcac-playwright package to be installed.
|
|
522
|
+
* Crawlee does not include it by default due to licensing issues.
|
|
523
|
+
*
|
|
524
|
+
* To use this method, please install the package manually by running:
|
|
525
|
+
*
|
|
526
|
+
* ```bash
|
|
527
|
+
* npm install idcac-playwright
|
|
528
|
+
* ```
|
|
513
529
|
*/
|
|
514
530
|
closeCookieModals(): Promise<void>;
|
|
515
531
|
/**
|
|
@@ -519,22 +535,44 @@ export interface PlaywrightContextUtils {
|
|
|
519
535
|
* result in a SessionError which will be automatically retried, so only successful requests will get
|
|
520
536
|
* into the `requestHandler`.
|
|
521
537
|
*
|
|
522
|
-
*
|
|
538
|
+
* On a successfully solved challenge the page is reloaded and the new {@link Response} is returned,
|
|
539
|
+
* which can be returned from the hook to update the crawling context's `response`. For the common case,
|
|
540
|
+
* prefer the pre-wrapped {@link handleCloudflareChallengeHook} hook.
|
|
523
541
|
*
|
|
524
542
|
* **Example usage**
|
|
525
543
|
* ```ts
|
|
526
544
|
* postNavigationHooks: [
|
|
527
|
-
* async ({ handleCloudflareChallenge })
|
|
528
|
-
* await handleCloudflareChallenge();
|
|
529
|
-
* },
|
|
545
|
+
* async (context) => ({ response: await context.handleCloudflareChallenge() }),
|
|
530
546
|
* ],
|
|
531
547
|
* ```
|
|
532
548
|
*
|
|
533
549
|
* @param [options]
|
|
534
550
|
*/
|
|
535
|
-
handleCloudflareChallenge(options?: HandleCloudflareChallengeOptions): Promise<
|
|
551
|
+
handleCloudflareChallenge(options?: HandleCloudflareChallengeOptions): Promise<Response | undefined>;
|
|
552
|
+
/**
|
|
553
|
+
* Returns the list of {@link https://playwright.dev/docs/api/class-download | Download} objects
|
|
554
|
+
* collected during the current page navigation and request handler.
|
|
555
|
+
*
|
|
556
|
+
* Useful for accessing files that the page downloads automatically.
|
|
557
|
+
* For most use cases, prefer re-enqueueing the URL to {@link FileDownload}.
|
|
558
|
+
* Use this only when direct access to the Playwright `Download` object is required.
|
|
559
|
+
*
|
|
560
|
+
* **Example usage**
|
|
561
|
+
* ```ts
|
|
562
|
+
* requestHandler: async ({ listDownloads }) => {
|
|
563
|
+
* for (const download of await listDownloads()) {
|
|
564
|
+
* try {
|
|
565
|
+
* const stream = await download.createReadStream();
|
|
566
|
+
* // stream to storage...
|
|
567
|
+
* } catch {
|
|
568
|
+
* // download failed or was cancelled
|
|
569
|
+
* }
|
|
570
|
+
* }
|
|
571
|
+
* },
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
listDownloads(): Promise<Download[]>;
|
|
536
575
|
}
|
|
537
|
-
export declare function registerUtilsToContext(context: PlaywrightCrawlingContext, crawlerOptions: PlaywrightCrawlerOptions): void;
|
|
538
576
|
export { enqueueLinksByClickingElements };
|
|
539
577
|
/** @internal */
|
|
540
578
|
export declare const playwrightUtils: {
|
|
@@ -551,4 +589,3 @@ export declare const playwrightUtils: {
|
|
|
551
589
|
RenderingTypePredictor: typeof RenderingTypePredictor;
|
|
552
590
|
handleCloudflareChallenge: typeof handleCloudflareChallenge;
|
|
553
591
|
};
|
|
554
|
-
//# sourceMappingURL=playwright-utils.d.ts.map
|