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't change user-agent on headless mode #448

Open
qphuongl opened this issue Apr 3, 2024 · 1 comment
Open

Can't change user-agent on headless mode #448

qphuongl opened this issue Apr 3, 2024 · 1 comment

Comments

@qphuongl
Copy link

qphuongl commented Apr 3, 2024

Hi, I tried to change UserAgent while running headless mode to avoid HeadlessChrome, but it was not working; here is my config:

  • BrowserTypeLaunchOptions:
Args: ["--disable-extensions","--disable-images","--window-size=1920,1080","--start-maximized", "--headless=new"]
HandleSIGUP, HandleSIGINT, HandleSIGTERM: false
IgnoreDefaultArgs: ["--enable-automation","--no-sandbox","--mute-audio","--headless","--disable-features"]

I ignored the default --headless flag because I want to use --headless=new to access chrome://version after trying --headless (failed).

  • BrowserNewContextOptions
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"

Actually, I tried to change userAgent in both headless and head mode but it's still not working. But right now, I'm dealing with headless mode.

Spec:

  • Chromium 123.0.6312.4 (Developer Build) (arm64)
  • Javascript: V8 12.3.219.1
  • OS: macOS version 14.4
@canstand
Copy link
Collaborator

Can't reproduce it. It worked fine in my test.

func TestHeadlessNew(t *testing.T) {
	b2, err := browserType.Launch(playwright.BrowserTypeLaunchOptions{
		Headless: playwright.Bool(false),
		Args: []string{
			"--disable-extensions", "--disable-images", "--window-size=1920,1080", "--start-maximized", "--headless=new",
		},
		IgnoreDefaultArgs: []string{"--enable-automation", "--no-sandbox", "--mute-audio", "--headless", "--disable-features"},
	})
	require.NoError(t, err)
	defer b2.Close()
	nCtx, err := b2.NewContext(playwright.BrowserNewContextOptions{
		UserAgent: playwright.String("foobar/1.0"),
	})
	require.NoError(t, err)
	defer nCtx.Close()
	nPage, err := nCtx.NewPage()
	require.NoError(t, err)

	resChan := server.WaitForRequestChan("/empty.html")
	_, err = nPage.Goto(server.PREFIX + "/empty.html")
	require.NoError(t, err)
	res := <-resChan
	require.True(t, strings.HasPrefix(res.Header.Get("User-Agent"), "foobar"))
}

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

2 participants