Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: use default user agent for playwright with chrome (#1350)

Martin Adámekd2d5ac7476e4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3c9d8da98..2fe87f7ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+2.3.2 / 2022/05/05
+====================
+- fix: use default user agent for playwright with chrome instead of the default "headless UA"
+- fix: always hide webdriver of chrome browsers
+
+
2.3.1 / 2022/05/03
====================
- fix: `utils.apifyClient` early instantiation (#1330)
diff --git a/package.json b/package.json
index 8e274024f..66a88821f 100644
--- a/package.json
+++ b/package.json
@@ -57,7 +57,7 @@
"@apify/timeout": "^0.2.1",
"@apify/utilities": "^1.2.8",
"apify-client": "^2.3.1",
- "browser-pool": "^3.1.2",
+ "browser-pool": "^3.1.3",
"cheerio": "1.0.0-rc.10",
"content-type": "^1.0.4",
"got-scraping": "^3.2.9",
diff --git a/src/browser_launchers/playwright_launcher.js b/src/browser_launchers/playwright_launcher.js
index e1756ced1..ddcebc2b6 100644
--- a/src/browser_launchers/playwright_launcher.js
+++ b/src/browser_launchers/playwright_launcher.js
@@ -2,6 +2,7 @@ import ow from 'ow';
import { LaunchOptions } from 'playwright'; // eslint-disable-line no-unused-vars
import { PlaywrightPlugin } from 'browser-pool';
import BrowserLauncher from './browser_launcher';
+import { DEFAULT_USER_AGENT } from '../constants';
/**
* Apify extends the launch options of Playwright.
@@ -32,6 +33,10 @@ import BrowserLauncher from './browser_launcher';
* and it may also contain proxy username and password.
*
* Example: `http://bob:[email protected]:1234`.
+ * @property {string} [userAgent]
+ * The `User-Agent` HTTP header used by the browser.
+ * If not provided, the function sets `User-Agent` to a reasonable default
+ * to reduce the chance of detection of the crawler.
* @property {boolean} [useChrome=false]
* If `true` and `executablePath` is not set,
* Playwright will launch full Google Chrome browser available on the machine
@@ -58,6 +63,7 @@ export class PlaywrightLauncher extends BrowserLauncher {
static optionsShape = {
...BrowserLauncher.optionsShape,
launcher: ow.optional.object,
+ userAgent: ow.optional.string,
};
/**
@@ -69,6 +75,7 @@ export class PlaywrightLauncher extends BrowserLauncher {
const {
launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium,
+ userAgent,
} = launchContext;
const { launchOptions = {}, ...rest } = launchContext;
@@ -82,8 +89,27 @@ export class PlaywrightLauncher extends BrowserLauncher {
launcher,
});
+ this.userAgent = userAgent;
this.Plugin = PlaywrightPlugin;
}
+
+ createLaunchOptions() {
+ const launchOptions = super.createLaunchOptions();
+ launchOptions.args = launchOptions.args || [];
+
+ // When User-Agent is not set and we're using Chromium or headless mode,
+ // it is better to use DEFAULT_USER_AGENT to reduce chance of detection
+ let { userAgent } = this;
+ if (!userAgent && (!launchOptions.executablePath || launchOptions.headless)) {
+ userAgent = DEFAULT_USER_AGENT;
+ }
+
+ if (userAgent) {
+ launchOptions.args.push(`--user-agent=${userAgent}`);
+ }
+
+ return launchOptions;
+ }
}
/**
diff --git a/src/constants.js b/src/constants.js
index fa99f7061..d4b1f1563 100644
--- a/src/constants.js
+++ b/src/constants.js
@@ -2,9 +2,9 @@ import { ACTOR_EVENT_NAMES } from '@apify/consts';
/**
* The default user agent used by `Apify.launchPuppeteer`.
- * Last updated on 2020-05-22.
+ * Last updated on 2022-05-05.
*/
-export const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36';
+export const DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36';
/**
* Exit codes for the actor process.
diff --git a/test/crawlers/playwright_crawler.test.js b/test/crawlers/playwright_crawler.test.js
index 63fc02ec0..5b06bcb53 100644
--- a/test/crawlers/playwright_crawler.test.js
+++ b/test/crawlers/playwright_crawler.test.js
@@ -70,6 +70,8 @@ describe('PlaywrightCrawler', () => {
expect(await response.status()).toBe(200);
request.userData.title = await page.title();
processed.push(request);
+ expect(response.request().headers()['user-agent']).not.toMatch(/headless/i);
+ await expect(page.evaluate(() => window.navigator.webdriver)).resolves.toBeFalsy();
};
const playwrightCrawler = new Apify.PlaywrightCrawler({
diff --git a/test/crawlers/puppeteer_crawler.test.js b/test/crawlers/puppeteer_crawler.test.js
index 8c80d21ae..553afdbd3 100644
--- a/test/crawlers/puppeteer_crawler.test.js
+++ b/test/crawlers/puppeteer_crawler.test.js
@@ -87,6 +87,8 @@ describe('PuppeteerCrawler', () => {
expect(await response.status()).toBe(200);
request.userData.title = await page.title();
processed.push(request);
+ expect(response.request().headers()['user-agent']).not.toMatch(/headless/i);
+ await expect(page.evaluate(() => window.navigator.webdriver)).resolves.toBeFalsy();
};
const puppeteerCrawler = new Apify.PuppeteerCrawler({