Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I set useragent like puppeteer? #697

Closed
trustyoo86 opened this issue Jan 28, 2020 · 6 comments
Closed

Can I set useragent like puppeteer? #697

trustyoo86 opened this issue Jan 28, 2020 · 6 comments

Comments

@trustyoo86
Copy link

I found this plugin while developing E2E server using puppeteer, So I considering replace it with this plugin.

But, I can't find page.setUserAgent function like puppeteer. My company use app using webview, So we add to the flag in useragent to identify it is an app.

So, Can I set useragent or add useragent use page object? If I can not add useragent, Are there any plans to add an useragent?

@aslushnikov
Copy link
Collaborator

Playwright's cornerstone is a "browser context" - all the setup goes through it. You can create a new browser context with a given user agent - and all pages inside of it will inherit it:

const {webkit} = require('playwright');
(async () => {
  const browser = await webkit.launch();
  // Create a new incognito browser context with a proper user agent
  const context = await browser.newContext({
    userAgent: 'my-user-agent'
  });
  // Now the page will have the user agent 
  const page = await context.newPage('https://example.com');
  console.log(await page.evaluate(() => navigator.userAgent));
})();

Check out docs for all the info.

@trustyoo86
Copy link
Author

Thx for my question. It helped a lot 👍

@buckthebug
Copy link

If the user agent is set with newContext() or newPage(), it's lost on navigation - goto() or click(). Only launchOptions and args works for me.
chromium & playwright 1.9.2

@yury-s
Copy link
Member

yury-s commented Mar 23, 2021

It works just fine for me:

  const context = await browser.newContext({userAgent: 'foo'});
  const p = await context.newPage();
  await p.goto('https://example.com');
  console.log(await p.evaluate('navigator.userAgent'));
  await p.goto('https://bing.com');
  console.log(await p.evaluate('navigator.userAgent'));

Please open a new issue with failing code snippet if it fails for you.

@buckthebug
Copy link

Thanks for your response. I've described it here: #5935
It's likely an edge case but if someone struggles like me, they can find a workaround there.

@AlexMarchini
Copy link

AlexMarchini commented Oct 25, 2023

Playwright's cornerstone is a "browser context" - all the setup goes through it. You can create a new browser context with a given user agent - and all pages inside of it will inherit it:

const {webkit} = require('playwright');
(async () => {
  const browser = await webkit.launch();
  // Create a new incognito browser context with a proper user agent
  const context = await browser.newContext({
    userAgent: 'my-user-agent'
  });
  // Now the page will have the user agent 
  const page = await context.newPage('https://example.com');
  console.log(await page.evaluate(() => navigator.userAgent));
})();

Check out docs for all the info.

@aslushnikov Is there a way to do this, but still inherit the global config settings in the new context? I noticed that by calling newContext() with a parameter, I'm losing the context options set in playwright.config.js. video: 'on' for example, will not be enabled in the new context although it's on in the default context. I want to just override the userAgent in the new context and preserve everything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants