Disable Javascript in Playwright

Viewed 1624

Is it possible to define a browser with Javascript disabled to simulate how a crawler would view a page? There should be an option that can be passed.

2 Answers

You can pass javaScriptEnabled in the BrowserContext options:

const playwright = require("playwright");

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext({
    javaScriptEnabled: false
  });
  const page = await context.newPage();
  // ...
  await browser.close();
})();

In the case of @playwright/test (where you don't define browser.newContext yourself) you can set javaScriptEnabled in testOptions.

(1) In the playwright.config.js file:

const config = {
  use: {
    headless: false,
    javaScriptEnabled: false
  },
};

module.exports = config;

(2) or with test.use:

const { test, expect } = require('@playwright/test');
test.use({ javaScriptEnabled: false });

test('website without JavaScript', async ({ page }) => {
  // ...
});
Related