@crawlee/playwright 4.0.0-beta.27 → 4.0.0-beta.270

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.
Files changed (32) hide show
  1. package/README.md +15 -15
  2. package/index.d.ts +4 -3
  3. package/index.js +2 -2
  4. package/internals/adaptive-playwright-crawler.d.ts +145 -74
  5. package/internals/adaptive-playwright-crawler.js +492 -296
  6. package/internals/enqueue-links/click-elements.d.ts +21 -58
  7. package/internals/enqueue-links/click-elements.js +58 -70
  8. package/internals/playwright-browser-pool.d.ts +55 -0
  9. package/internals/playwright-browser-pool.js +34 -0
  10. package/internals/playwright-crawler.d.ts +179 -149
  11. package/internals/playwright-crawler.js +93 -79
  12. package/internals/playwright-launcher.d.ts +30 -20
  13. package/internals/playwright-launcher.js +24 -18
  14. package/internals/utils/playwright-utils.d.ts +64 -56
  15. package/internals/utils/playwright-utils.js +117 -138
  16. package/internals/utils/rendering-type-prediction.d.ts +34 -14
  17. package/internals/utils/rendering-type-prediction.js +87 -35
  18. package/package.json +19 -22
  19. package/index.d.ts.map +0 -1
  20. package/index.js.map +0 -1
  21. package/internals/adaptive-playwright-crawler.d.ts.map +0 -1
  22. package/internals/adaptive-playwright-crawler.js.map +0 -1
  23. package/internals/enqueue-links/click-elements.d.ts.map +0 -1
  24. package/internals/enqueue-links/click-elements.js.map +0 -1
  25. package/internals/playwright-crawler.d.ts.map +0 -1
  26. package/internals/playwright-crawler.js.map +0 -1
  27. package/internals/playwright-launcher.d.ts.map +0 -1
  28. package/internals/playwright-launcher.js.map +0 -1
  29. package/internals/utils/playwright-utils.d.ts.map +0 -1
  30. package/internals/utils/playwright-utils.js.map +0 -1
  31. package/internals/utils/rendering-type-prediction.d.ts.map +0 -1
  32. package/internals/utils/rendering-type-prediction.js.map +0 -1
@@ -1,7 +1,8 @@
1
- import { BrowserCrawler, Configuration, RequestState, Router } from '@crawlee/browser';
2
- import ow from 'ow';
3
- import { PlaywrightLauncher } from './playwright-launcher.js';
4
- import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
1
+ import { BrowserCrawler, RequestState, Router, serviceLocator } from '@crawlee/browser';
2
+ import { assertBrowserPoolNotConfigured, parseArgument, schemas } from '@crawlee/utils/internal';
3
+ import { z } from 'zod';
4
+ import { playwrightBrowserPool } from './playwright-browser-pool.js';
5
+ import { blockRequests, compileScript, enqueueLinksByClickingElements, gotoExtended, handleCloudflareChallenge, infiniteScroll, injectFile, injectJQuery, parseWithCheerio, saveSnapshot, } from './utils/playwright-utils.js';
5
6
  /**
6
7
  * Provides a simple framework for parallel crawling of web pages
7
8
  * using headless Chromium, Firefox and Webkit browsers with [Playwright](https://github.com/microsoft/playwright).
@@ -13,24 +14,26 @@ import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
13
14
  * If the target website doesn't need JavaScript, consider using {@link CheerioCrawler},
14
15
  * which downloads the pages using raw HTTP requests and is about 10x faster.
15
16
  *
16
- * The source URLs are represented using {@link Request} objects that are fed from
17
- * {@link RequestList} or {@link RequestQueue} instances provided by the {@link PlaywrightCrawlerOptions.requestList}
18
- * or {@link PlaywrightCrawlerOptions.requestQueue} constructor options, respectively.
17
+ * The source URLs are represented using {@link Request} objects that are fed from the
18
+ * {@link IRequestManager|request manager} provided via the {@link PlaywrightCrawlerOptions.requestManager|`requestManager`}
19
+ * constructor option (a {@link RequestQueue} is itself a request manager). To read from a read-only source such
20
+ * as a {@link RequestList} while still being able to enqueue new requests, combine it with a queue into a
21
+ * {@link RequestManagerTandem} via {@link IRequestLoader.toTandem|`requestLoader.toTandem()`} and pass the
22
+ * result as `requestManager`.
19
23
  *
20
- * If both {@link PlaywrightCrawlerOptions.requestList} and {@link PlaywrightCrawlerOptions.requestQueue} are used,
21
- * the instance first processes URLs from the {@link RequestList} and automatically enqueues all of them
22
- * to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
24
+ * > The {@link PlaywrightCrawlerOptions.requestList|`requestList`} and {@link PlaywrightCrawlerOptions.requestQueue|`requestQueue`}
25
+ * > options are deprecated; they are still accepted and folded into a single `requestManager` for back-compat.
23
26
  *
24
27
  * The crawler finishes when there are no more {@link Request} objects to crawl.
25
28
  *
26
29
  * `PlaywrightCrawler` opens a new Chrome page (i.e. tab) for each {@link Request} object to crawl
27
30
  * and then calls the function provided by user as the {@link PlaywrightCrawlerOptions.requestHandler} option.
28
31
  *
29
- * New pages are only opened when there is enough free CPU and memory available,
30
- * using the functionality provided by the {@link AutoscaledPool} class.
31
- * All {@link AutoscaledPool} configuration options can be passed to the {@link PlaywrightCrawlerOptions.autoscaledPoolOptions}
32
- * parameter of the `PlaywrightCrawler` constructor. For user convenience, the `minConcurrency` and `maxConcurrency`
33
- * {@link AutoscaledPoolOptions} are available directly in the `PlaywrightCrawler` constructor.
32
+ * New pages are only opened when there is enough free CPU and memory available, as judged by the crawler's
33
+ * {@link ConcurrencySystem}.
34
+ * Concurrency is tuned via the `minConcurrency`, `maxConcurrency` and `maxRequestsPerMinute` options of the
35
+ * `PlaywrightCrawler` constructor, or, for finer control, by injecting a pre-configured
36
+ * {@link ConcurrencySystem|`concurrencySystem`}.
34
37
  *
35
38
  * Note that the pool of Playwright instances is internally managed by the [BrowserPool](https://github.com/apify/browser-pool) class.
36
39
  *
@@ -66,112 +69,123 @@ import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
66
69
  * @category Crawlers
67
70
  */
68
71
  export class PlaywrightCrawler extends BrowserCrawler {
69
- config;
72
+ /**
73
+ * @internal
74
+ */
70
75
  static optionsShape = {
71
76
  ...BrowserCrawler.optionsShape,
72
- browserPoolOptions: ow.optional.object,
73
- launcher: ow.optional.object,
74
- ignoreIframes: ow.optional.boolean,
75
- ignoreShadowRoots: ow.optional.boolean,
77
+ launchContext: schemas.anyObject.default(() => ({})),
78
+ headless: z.boolean().optional(),
76
79
  };
80
+ /** @internal */
81
+ static optionsSchema = z.strictObject(PlaywrightCrawler.optionsShape);
77
82
  /**
78
83
  * All `PlaywrightCrawler` parameters are passed via an options object.
79
84
  */
80
- constructor(options = {}, config = Configuration.getGlobalConfig()) {
81
- ow(options, 'PlaywrightCrawlerOptions', ow.object.exactShape(PlaywrightCrawler.optionsShape));
82
- const { launchContext = {}, headless, ...browserCrawlerOptions } = options;
83
- const browserPoolOptions = {
84
- ...options.browserPoolOptions,
85
- };
85
+ constructor(options = {}) {
86
+ const parsedOptions = parseArgument(options, PlaywrightCrawler.optionsSchema, 'PlaywrightCrawlerOptions');
87
+ const { launchContext, headless, configuration, ...browserCrawlerOptions } = parsedOptions;
86
88
  if (launchContext.proxyUrl) {
87
89
  throw new Error('PlaywrightCrawlerOptions.launchContext.proxyUrl is not allowed in PlaywrightCrawler.' +
88
90
  'Use PlaywrightCrawlerOptions.proxyConfiguration');
89
91
  }
90
- // `browserPlugins` is working when it's not overridden by `launchContext`,
91
- // which for crawlers it is always overridden. Hence the error to use the other option.
92
- if (browserPoolOptions.browserPlugins) {
93
- throw new Error('browserPoolOptions.browserPlugins is disallowed. Use launchContext.launcher instead.');
92
+ if (options.browserPool) {
93
+ // The raw options, not the parsed ones: `launchContext` has a default, so by now it is always set.
94
+ assertBrowserPoolNotConfigured(new.target.name, {
95
+ launchContext: options.launchContext,
96
+ headless: options.headless,
97
+ });
94
98
  }
95
- if (headless != null) {
96
- launchContext.launchOptions ??= {};
97
- launchContext.launchOptions.headless = headless;
98
- }
99
- const playwrightLauncher = new PlaywrightLauncher(launchContext, config);
100
- browserPoolOptions.browserPlugins = [playwrightLauncher.createBrowserPlugin()];
101
99
  super({
102
100
  ...browserCrawlerOptions,
103
- launchContext,
104
- browserPoolOptions,
105
- contextPipelineBuilder: () => this.buildContextPipeline().compose({ action: this.enhanceContext.bind(this) }),
106
- }, config);
107
- this.config = config;
101
+ configuration,
102
+ browserPoolBuilder: (remoteBrowser) => playwrightBrowserPool({ launchContext, headless, configuration, remoteBrowser }),
103
+ contextPipelineBuilder: () => this.#buildContextPipeline(),
104
+ });
105
+ }
106
+ #buildContextPipeline() {
107
+ return this.buildContextPipeline().compose(this.#enhanceContext.bind(this));
108
108
  }
109
- async _navigationHandler(crawlingContext, gotoOptions) {
109
+ async navigationHandler(crawlingContext, gotoOptions) {
110
110
  return gotoExtended(crawlingContext.page, crawlingContext.request, gotoOptions);
111
111
  }
112
- async enhanceContext(context) {
112
+ async #enhanceContext(context) {
113
113
  const waitForSelector = async (selector, timeoutMs = 5_000) => {
114
114
  const locator = context.page.locator(selector).first();
115
115
  await locator.waitFor({ timeout: timeoutMs, state: 'attached' });
116
116
  };
117
+ const parseWithCheerioFn = async (selector, timeoutMs = 5_000) => {
118
+ if (selector) {
119
+ await waitForSelector(selector, timeoutMs);
120
+ }
121
+ return parseWithCheerio(context.page, this.ignoreShadowRoots, this.ignoreIframes);
122
+ };
123
+ const querySelectorAll = async (selector, timeoutMs) => {
124
+ const matches = (await parseWithCheerioFn(selector, timeoutMs))(selector);
125
+ // The browser located it but the Cheerio snapshot does not have it - a Playwright-only selector,
126
+ // or an element under a shadow root/iframe the snapshot was told to ignore.
127
+ if (matches.length === 0) {
128
+ throw new Error(`Selector '${selector}' not found in the page snapshot.`);
129
+ }
130
+ return matches;
131
+ };
132
+ const downloads = [];
133
+ context.page.on('download', (download) => downloads.push(download));
117
134
  return {
118
- injectFile: async (filePath, options) => playwrightUtils.injectFile(context.page, filePath, options),
135
+ injectFile: async (filePath, options) => injectFile(context.page, filePath, options),
119
136
  injectJQuery: async () => {
120
137
  if (context.request.state === RequestState.BEFORE_NAV) {
121
138
  context.log.warning('Using injectJQuery() in preNavigationHooks leads to unstable results. Use it in a postNavigationHook or a requestHandler instead.');
122
- await playwrightUtils.injectJQuery(context.page);
139
+ await injectJQuery(context.page);
123
140
  return;
124
141
  }
125
- await playwrightUtils.injectJQuery(context.page, { surviveNavigations: false });
142
+ await injectJQuery(context.page, { surviveNavigations: false });
126
143
  },
127
- blockRequests: async (options) => playwrightUtils.blockRequests(context.page, options),
144
+ blockRequests: async (options) => blockRequests(context.page, options),
128
145
  waitForSelector,
129
- parseWithCheerio: async (selector, timeoutMs = 5_000) => {
130
- if (selector) {
131
- await waitForSelector(selector, timeoutMs);
132
- }
133
- return playwrightUtils.parseWithCheerio(context.page, this.ignoreShadowRoots, this.ignoreIframes);
134
- },
135
- infiniteScroll: async (options) => playwrightUtils.infiniteScroll(context.page, options),
136
- saveSnapshot: async (options) => playwrightUtils.saveSnapshot(context.page, { ...options, config: this.config }),
137
- enqueueLinksByClickingElements: async (options) => playwrightUtils.enqueueLinksByClickingElements({
146
+ parseWithCheerio: parseWithCheerioFn,
147
+ querySelectorAll,
148
+ querySelectorOne: async (selector, timeoutMs) => (await querySelectorAll(selector, timeoutMs)).first(),
149
+ infiniteScroll: async (options) => infiniteScroll(context.page, options),
150
+ listDownloads: async () => downloads,
151
+ saveSnapshot: async (options) => saveSnapshot(context.page, {
152
+ ...options,
153
+ configuration: serviceLocator.getConfiguration(),
154
+ }),
155
+ enqueueLinksByClickingElements: async (options) => enqueueLinksByClickingElements({
138
156
  ...options,
139
157
  page: context.page,
140
- requestQueue: this.requestQueue,
158
+ requestManager: this.requestManager,
141
159
  }),
142
- compileScript: (scriptString, ctx) => playwrightUtils.compileScript(scriptString, ctx),
143
- closeCookieModals: async () => playwrightUtils.closeCookieModals(context.page),
160
+ compileScript: (scriptString, ctx) => compileScript(scriptString, ctx),
144
161
  handleCloudflareChallenge: async (options) => {
145
- return playwrightUtils.handleCloudflareChallenge(context.page, context.request.url, context.session, options);
162
+ return handleCloudflareChallenge(context.page, context.request.url, options);
146
163
  },
147
164
  };
148
165
  }
149
166
  }
150
167
  /**
151
- * Creates new {@link Router} instance that works based on request labels.
152
- * This instance can then serve as a `requestHandler` of your {@link PlaywrightCrawler}.
153
- * Defaults to the {@link PlaywrightCrawlingContext}.
154
- *
155
- * > Serves as a shortcut for using `Router.create<PlaywrightCrawlingContext>()`.
168
+ * Returns a `postNavigationHooks`-ready hook that runs {@link PlaywrightContextUtils.handleCloudflareChallenge}
169
+ * and propagates the post-challenge {@link Response} back into the crawling context via its return value.
156
170
  *
171
+ * **Example usage**
157
172
  * ```ts
158
- * import { PlaywrightCrawler, createPlaywrightRouter } from 'crawlee';
159
- *
160
- * const router = createPlaywrightRouter();
161
- * router.addHandler('label-a', async (ctx) => {
162
- * ctx.log.info('...');
163
- * });
164
- * router.addDefaultHandler(async (ctx) => {
165
- * ctx.log.info('...');
166
- * });
173
+ * import { PlaywrightCrawler, handleCloudflareChallengeHook } from 'crawlee';
167
174
  *
168
175
  * const crawler = new PlaywrightCrawler({
169
- * requestHandler: router,
176
+ * postNavigationHooks: [handleCloudflareChallengeHook()],
170
177
  * });
171
- * await crawler.run();
172
178
  * ```
173
179
  */
174
- export function createPlaywrightRouter(routes) {
175
- return Router.create(routes);
180
+ export function handleCloudflareChallengeHook(options) {
181
+ return async (context) => {
182
+ const response = await context.handleCloudflareChallenge(options);
183
+ if (response !== undefined) {
184
+ return { response };
185
+ }
186
+ return undefined;
187
+ };
188
+ }
189
+ export function createPlaywrightRouter(routesOrSchemas) {
190
+ return Router.create(routesOrSchemas);
176
191
  }
177
- //# sourceMappingURL=playwright-crawler.js.map
@@ -3,6 +3,7 @@ import { BrowserLauncher, Configuration } from '@crawlee/browser';
3
3
  import { PlaywrightPlugin } from '@crawlee/browser-pool';
4
4
  // @ts-ignore optional peer dependency or compatibility with es2022
5
5
  import type { Browser, BrowserType, LaunchOptions } from 'playwright';
6
+ import { z } from 'zod';
6
7
  /**
7
8
  * Apify extends the launch options of Playwright.
8
9
  * You can use any of the Playwright compatible
@@ -70,31 +71,41 @@ export interface PlaywrightLaunchContext extends BrowserLaunchContext<LaunchOpti
70
71
  * @ignore
71
72
  */
72
73
  export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin> {
73
- readonly config: Configuration;
74
+ readonly configuration: Configuration;
75
+ /**
76
+ * @internal
77
+ */
74
78
  protected static optionsShape: {
79
+ proxyUrl: z.ZodOptional<z.ZodURL>;
80
+ useChrome: z.ZodOptional<z.ZodBoolean>;
81
+ useIncognitoPages: z.ZodOptional<z.ZodBoolean>;
82
+ browserPerProxy: z.ZodOptional<z.ZodBoolean>;
83
+ ignoreProxyCertificate: z.ZodOptional<z.ZodBoolean>;
84
+ userDataDir: z.ZodOptional<z.ZodString>;
75
85
  // @ts-ignore optional peer dependency or compatibility with es2022
76
- launcher: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
77
- // @ts-ignore optional peer dependency or compatibility with es2022
78
- launchContextOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
79
- // @ts-ignore optional peer dependency or compatibility with es2022
80
- proxyUrl: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
81
- // @ts-ignore optional peer dependency or compatibility with es2022
82
- useChrome: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
86
+ launchOptions: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
87
+ userAgent: z.ZodOptional<z.ZodString>;
83
88
  // @ts-ignore optional peer dependency or compatibility with es2022
84
- useIncognitoPages: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
85
- // @ts-ignore optional peer dependency or compatibility with es2022
86
- browserPerProxy: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
87
- // @ts-ignore optional peer dependency or compatibility with es2022
88
- userDataDir: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
89
+ launcher: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
90
+ };
91
+ /** @internal */
92
+ protected static optionsSchema: z.ZodObject<{
93
+ proxyUrl: z.ZodOptional<z.ZodURL>;
94
+ useChrome: z.ZodOptional<z.ZodBoolean>;
95
+ useIncognitoPages: z.ZodOptional<z.ZodBoolean>;
96
+ browserPerProxy: z.ZodOptional<z.ZodBoolean>;
97
+ ignoreProxyCertificate: z.ZodOptional<z.ZodBoolean>;
98
+ userDataDir: z.ZodOptional<z.ZodString>;
89
99
  // @ts-ignore optional peer dependency or compatibility with es2022
90
- launchOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
100
+ launchOptions: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
101
+ userAgent: z.ZodOptional<z.ZodString>;
91
102
  // @ts-ignore optional peer dependency or compatibility with es2022
92
- userAgent: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
93
- };
103
+ launcher: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
104
+ }, z.core.$strict>;
94
105
  /**
95
106
  * All `PlaywrightLauncher` parameters are passed via this launchContext object.
96
107
  */
97
- constructor(launchContext?: PlaywrightLaunchContext, config?: Configuration);
108
+ constructor(launchContext?: PlaywrightLaunchContext, configuration?: Configuration);
98
109
  }
99
110
  /**
100
111
  * Launches headless browsers using Playwright pre-configured to work within the Apify platform.
@@ -125,9 +136,8 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
125
136
  * Optional settings passed to `browserType.launch()`. In addition to
126
137
  * [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
127
138
  * the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
128
- * @param [config]
139
+ * @param [configuration]
129
140
  * @returns
130
141
  * Promise that resolves to Playwright's `Browser` instance.
131
142
  */
132
- export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext, config?: Configuration): Promise<Browser>;
133
- //# sourceMappingURL=playwright-launcher.d.ts.map
143
+ export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext, configuration?: Configuration): Promise<Browser>;
@@ -1,34 +1,41 @@
1
1
  import { BrowserLauncher, Configuration } from '@crawlee/browser';
2
- import { PlaywrightPlugin } from '@crawlee/browser-pool';
3
- import ow from 'ow';
2
+ import { PlaywrightPlugin, RemotePlaywrightPlugin } from '@crawlee/browser-pool';
3
+ import { parseArgument, schemas } from '@crawlee/utils/internal';
4
+ import { z } from 'zod';
4
5
  /**
5
6
  * `PlaywrightLauncher` is based on the `BrowserLauncher`. It launches `playwright` browser instance.
6
7
  * @ignore
7
8
  */
8
9
  export class PlaywrightLauncher extends BrowserLauncher {
9
- config;
10
+ configuration;
11
+ /**
12
+ * @internal
13
+ */
10
14
  static optionsShape = {
11
15
  ...BrowserLauncher.optionsShape,
12
- launcher: ow.optional.object,
13
- launchContextOptions: ow.optional.object,
16
+ // Passthrough schema — the launcher module object must keep its prototype through parsing.
17
+ launcher: schemas.anyObject.optional(),
14
18
  };
19
+ /** @internal */
20
+ static optionsSchema = z.strictObject(PlaywrightLauncher.optionsShape);
15
21
  /**
16
22
  * All `PlaywrightLauncher` parameters are passed via this launchContext object.
17
23
  */
18
- constructor(launchContext = {}, config = Configuration.getGlobalConfig()) {
19
- ow(launchContext, 'PlaywrightLauncherOptions', ow.object.exactShape(PlaywrightLauncher.optionsShape));
20
- const { launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium, } = launchContext;
21
- const { launchOptions = {}, ...rest } = launchContext;
24
+ constructor(launchContext = {}, configuration = Configuration.getGlobalConfiguration()) {
25
+ const parsedContext = parseArgument(launchContext, PlaywrightLauncher.optionsSchema, 'PlaywrightLaunchContext');
26
+ const { launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium, } = parsedContext;
27
+ const { launchOptions = {}, ...rest } = parsedContext;
22
28
  super({
23
29
  ...rest,
24
30
  launchOptions: {
25
31
  ...launchOptions,
26
- executablePath: getDefaultExecutablePath(launchContext, config),
32
+ executablePath: getDefaultExecutablePath(parsedContext, configuration),
27
33
  },
28
34
  launcher,
29
- }, config);
30
- this.config = config;
35
+ }, configuration);
36
+ this.configuration = configuration;
31
37
  this.Plugin = PlaywrightPlugin;
38
+ this.RemotePlugin = RemotePlaywrightPlugin;
32
39
  }
33
40
  }
34
41
  /**
@@ -36,8 +43,8 @@ export class PlaywrightLauncher extends BrowserLauncher {
36
43
  * @returns default path to browser.
37
44
  * @ignore
38
45
  */
39
- function getDefaultExecutablePath(launchContext, config) {
40
- const pathFromPlaywrightImage = config.get('defaultBrowserPath');
46
+ function getDefaultExecutablePath(launchContext, configuration) {
47
+ const pathFromPlaywrightImage = configuration.defaultBrowserPath;
41
48
  const { launchOptions = {} } = launchContext;
42
49
  if (launchOptions.executablePath) {
43
50
  return launchOptions.executablePath;
@@ -79,12 +86,11 @@ function getDefaultExecutablePath(launchContext, config) {
79
86
  * Optional settings passed to `browserType.launch()`. In addition to
80
87
  * [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
81
88
  * the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
82
- * @param [config]
89
+ * @param [configuration]
83
90
  * @returns
84
91
  * Promise that resolves to Playwright's `Browser` instance.
85
92
  */
86
- export async function launchPlaywright(launchContext, config = Configuration.getGlobalConfig()) {
87
- const playwrightLauncher = new PlaywrightLauncher(launchContext, config);
93
+ export async function launchPlaywright(launchContext, configuration = Configuration.getGlobalConfiguration()) {
94
+ const playwrightLauncher = new PlaywrightLauncher(launchContext, configuration);
88
95
  return playwrightLauncher.launch();
89
96
  }
90
- //# sourceMappingURL=playwright-launcher.js.map
@@ -17,14 +17,14 @@
17
17
  * ```
18
18
  * @module playwrightUtils
19
19
  */
20
- import { Configuration, type Request, type Session } from '@crawlee/browser';
21
- import type { BatchAddRequestsResult } from '@crawlee/types';
22
- import { type CheerioRoot, type Dictionary } from '@crawlee/utils';
20
+ import { Configuration, type Request } from '@crawlee/browser';
21
+ import type { BatchAddRequestsResult, Dictionary } from '@crawlee/types';
22
+ import type { Cheerio, CheerioAPI } from 'cheerio';
23
+ import type { AnyNode } from 'domhandler';
23
24
  // @ts-ignore optional peer dependency or compatibility with es2022
24
- import type { Page, Response } from 'playwright';
25
+ import type { Download, Page, Response } from 'playwright';
25
26
  import type { EnqueueLinksByClickingElementsOptions } from '../enqueue-links/click-elements.js';
26
27
  import { enqueueLinksByClickingElements } from '../enqueue-links/click-elements.js';
27
- import { RenderingTypePredictor } from './rendering-type-prediction.js';
28
28
  export interface InjectFileOptions {
29
29
  /**
30
30
  * Enables the injected script to survive page navigations and reloads without need to be re-injected manually.
@@ -265,9 +265,9 @@ export interface SaveSnapshotOptions {
265
265
  keyValueStoreName?: string | null;
266
266
  /**
267
267
  * Configuration of the crawler that will be used to save the snapshot.
268
- * @default Configuration.getGlobalConfig()
268
+ * @default Configuration.getGlobalConfiguration()
269
269
  */
270
- config?: Configuration;
270
+ configuration?: Configuration;
271
271
  }
272
272
  /**
273
273
  * Saves a full screenshot and HTML of the current page into a Key-Value store.
@@ -287,8 +287,7 @@ export declare function saveSnapshot(page: Page, options?: SaveSnapshotOptions):
287
287
  * @param page Playwright [`Page`](https://playwright.dev/docs/api/class-page) object.
288
288
  * @param ignoreShadowRoots
289
289
  */
290
- export declare function parseWithCheerio(page: Page, ignoreShadowRoots?: boolean, ignoreIframes?: boolean): Promise<CheerioRoot>;
291
- export declare function closeCookieModals(page: Page): Promise<void>;
290
+ export declare function parseWithCheerio(page: Page, ignoreShadowRoots?: boolean, ignoreIframes?: boolean): Promise<CheerioAPI>;
292
291
  export interface HandleCloudflareChallengeOptions {
293
292
  /** Logging defaults to the `debug` level, use this flag to log to `info` level instead. */
294
293
  verbose?: boolean;
@@ -303,6 +302,13 @@ export interface HandleCloudflareChallengeOptions {
303
302
  isChallengeCallback?: (page: Page) => Promise<boolean>;
304
303
  /** Allows overriding the detection of Cloudflare "blocked page". */
305
304
  isBlockedCallback?: (page: Page) => Promise<boolean>;
305
+ /** Allows overriding how the checkbox click position is calculated. */
306
+ clickPositionCallback?: (page: Page) => Promise<{
307
+ x: number;
308
+ y: number;
309
+ } | null>;
310
+ /** Optional delay (in seconds) before the first click attempt on the challenge checkbox. Defaults to 1s. */
311
+ preChallengeSleepSecs?: number;
306
312
  }
307
313
  /**
308
314
  * This helper tries to solve the Cloudflare challenge automatically by clicking on the checkbox.
@@ -311,24 +317,24 @@ export interface HandleCloudflareChallengeOptions {
311
317
  * result in a SessionError which will be automatically retried, so only successful requests will get
312
318
  * into the `requestHandler`.
313
319
  *
320
+ * On a successfully solved challenge the page is reloaded and the new {@link Response} is returned, so
321
+ * it can be propagated back to the crawling context via a hook return value (see
322
+ * {@link handleCloudflareChallengeHook}).
323
+ *
314
324
  * Works best with camoufox.
315
325
  *
316
326
  * **Example usage**
317
327
  * ```ts
318
328
  * postNavigationHooks: [
319
- * async ({ handleCloudflareChallenge }) => {
320
- * await handleCloudflareChallenge();
321
- * },
329
+ * async (context) => ({ response: await context.handleCloudflareChallenge() }),
322
330
  * ],
323
331
  * ```
324
332
  *
325
333
  * @param page Playwright [`Page`](https://playwright.dev/docs/api/class-page) object
326
334
  * @param url current URL for request identification, only used for logging
327
- * @param [session] current session object
328
335
  * @param [options]
329
336
  */
330
- declare function handleCloudflareChallenge(page: Page, url: string, session?: Session, options?: HandleCloudflareChallengeOptions): Promise<void>;
331
- /** @internal */
337
+ export declare function handleCloudflareChallenge(page: Page, url: string, options?: HandleCloudflareChallengeOptions): Promise<Response | undefined>;
332
338
  export interface PlaywrightContextUtils {
333
339
  /**
334
340
  * Injects a JavaScript file into current `page`.
@@ -415,6 +421,15 @@ export interface PlaywrightContextUtils {
415
421
  * ```
416
422
  */
417
423
  waitForSelector(selector: string, timeoutMs?: number): Promise<void>;
424
+ /**
425
+ * Like {@link PlaywrightContextUtils.waitForSelector|`waitForSelector`}, but returns the matched elements
426
+ * as a Cheerio object. Throws when nothing matches.
427
+ */
428
+ querySelectorAll(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
429
+ /**
430
+ * Like {@link PlaywrightContextUtils.querySelectorAll|`querySelectorAll`}, but returns only the first match.
431
+ */
432
+ querySelectorOne(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
418
433
  /**
419
434
  * Returns Cheerio handle for `page.content()`, allowing to work with the data same way as with {@link CheerioCrawler}.
420
435
  * When provided with the `selector` argument, it waits for it to be available first.
@@ -427,7 +442,7 @@ export interface PlaywrightContextUtils {
427
442
  * });
428
443
  * ```
429
444
  */
430
- parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioRoot>;
445
+ parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioAPI>;
431
446
  /**
432
447
  * Scrolls to the bottom of a page, or until it times out.
433
448
  * Loads dynamic content when it hits the bottom of a page, and then continues scrolling.
@@ -447,8 +462,7 @@ export interface PlaywrightContextUtils {
447
462
  * in `href` elements, but rather navigations are triggered in click handlers.
448
463
  * If you're looking to find URLs in `href` attributes of the page, see {@link enqueueLinks}.
449
464
  *
450
- * Optionally, the function allows you to filter the target links' URLs using an array of {@link PseudoUrl} objects
451
- * and override settings of the enqueued {@link Request} objects.
465
+ * Optionally, the function allows you to filter the target links' URLs using an array of glob or regexp patterns.
452
466
  *
453
467
  * **IMPORTANT**: To be able to do this, this function uses various mutations on the page,
454
468
  * such as changing the Z-index of elements being clicked and their visibility. Therefore,
@@ -469,9 +483,9 @@ export interface PlaywrightContextUtils {
469
483
  * async requestHandler({ enqueueLinksByClickingElements }) {
470
484
  * await enqueueLinksByClickingElements({
471
485
  * selector: 'a.product-detail',
472
- * globs: [
473
- * 'https://www.example.com/handbags/**'
474
- * 'https://www.example.com/purses/**'
486
+ * include: [
487
+ * 'https://www.example.com/handbags/**',
488
+ * 'https://www.example.com/purses/**',
475
489
  * ],
476
490
  * });
477
491
  * });
@@ -479,7 +493,7 @@ export interface PlaywrightContextUtils {
479
493
  *
480
494
  * @returns Promise that resolves to {@link BatchAddRequestsResult} object.
481
495
  */
482
- enqueueLinksByClickingElements(options: Omit<EnqueueLinksByClickingElementsOptions, 'page' | 'requestQueue'>): Promise<BatchAddRequestsResult>;
496
+ enqueueLinksByClickingElements(options: Omit<EnqueueLinksByClickingElementsOptions, 'page' | 'requestManager'>): Promise<BatchAddRequestsResult>;
483
497
  /**
484
498
  * Compiles a Playwright script into an async function that may be executed at any time
485
499
  * by providing it with the following object:
@@ -507,19 +521,6 @@ export interface PlaywrightContextUtils {
507
521
  * secured copies beforehand.
508
522
  */
509
523
  compileScript(scriptString: string, ctx?: Dictionary): CompiledScriptFunction;
510
- /**
511
- * Tries to close cookie consent modals on the page. Based on the I Don't Care About Cookies browser extension.
512
- *
513
- * Note that this method requires the idcac-playwright package to be installed.
514
- * Crawlee does not include it by default due to licensing issues.
515
- *
516
- * To use this method, please install the package manually by running:
517
- *
518
- * ```bash
519
- * npm install idcac-playwright
520
- * ```
521
- */
522
- closeCookieModals(): Promise<void>;
523
524
  /**
524
525
  * This helper tries to solve the Cloudflare challenge automatically by clicking on the checkbox.
525
526
  * It will try to detect the Cloudflare page, click on the checkbox, and wait for 10 seconds (configurable
@@ -527,35 +528,42 @@ export interface PlaywrightContextUtils {
527
528
  * result in a SessionError which will be automatically retried, so only successful requests will get
528
529
  * into the `requestHandler`.
529
530
  *
530
- * Works best with camoufox.
531
+ * On a successfully solved challenge the page is reloaded and the new {@link Response} is returned,
532
+ * which can be returned from the hook to update the crawling context's `response`. For the common case,
533
+ * prefer the pre-wrapped {@link handleCloudflareChallengeHook} hook.
531
534
  *
532
535
  * **Example usage**
533
536
  * ```ts
534
537
  * postNavigationHooks: [
535
- * async ({ handleCloudflareChallenge }) => {
536
- * await handleCloudflareChallenge();
537
- * },
538
+ * async (context) => ({ response: await context.handleCloudflareChallenge() }),
538
539
  * ],
539
540
  * ```
540
541
  *
541
542
  * @param [options]
542
543
  */
543
- handleCloudflareChallenge(options?: HandleCloudflareChallengeOptions): Promise<void>;
544
+ handleCloudflareChallenge(options?: HandleCloudflareChallengeOptions): Promise<Response | undefined>;
545
+ /**
546
+ * Returns the list of {@link https://playwright.dev/docs/api/class-download | Download} objects
547
+ * collected during the current page navigation and request handler.
548
+ *
549
+ * Useful for accessing files that the page downloads automatically.
550
+ * For most use cases, prefer re-enqueueing the URL to {@link FileDownload}.
551
+ * Use this only when direct access to the Playwright `Download` object is required.
552
+ *
553
+ * **Example usage**
554
+ * ```ts
555
+ * requestHandler: async ({ listDownloads }) => {
556
+ * for (const download of await listDownloads()) {
557
+ * try {
558
+ * const stream = await download.createReadStream();
559
+ * // stream to storage...
560
+ * } catch {
561
+ * // download failed or was cancelled
562
+ * }
563
+ * }
564
+ * },
565
+ * ```
566
+ */
567
+ listDownloads(): Promise<Download[]>;
544
568
  }
545
569
  export { enqueueLinksByClickingElements };
546
- /** @internal */
547
- export declare const playwrightUtils: {
548
- injectFile: typeof injectFile;
549
- injectJQuery: typeof injectJQuery;
550
- gotoExtended: typeof gotoExtended;
551
- blockRequests: typeof blockRequests;
552
- enqueueLinksByClickingElements: typeof enqueueLinksByClickingElements;
553
- parseWithCheerio: typeof parseWithCheerio;
554
- infiniteScroll: typeof infiniteScroll;
555
- saveSnapshot: typeof saveSnapshot;
556
- compileScript: typeof compileScript;
557
- closeCookieModals: typeof closeCookieModals;
558
- RenderingTypePredictor: typeof RenderingTypePredictor;
559
- handleCloudflareChallenge: typeof handleCloudflareChallenge;
560
- };
561
- //# sourceMappingURL=playwright-utils.d.ts.map