Changing user agent on headless chrome
Asked Answered
B

1

6

I have an issue with changing the user agent.

I am trying to use the following line in my runner.js file in the browsers array :

chrome:headless:userAgent=Mozilla/5.0\ \(Linux\;\ Android\ 5.0\;\ SM-G900P\ Build/LRX21T\)\ AppleWebKit/537.36\ \(KHTML,\ like\ Gecko\)\ Chrome/57.0.2987.133\ Mobile\ Safari/537.36

However, the best I can get is Mozilla/5.0 (Linux in the actual user agent.

The guide doesn't say anything explicit about user agents and how to escape them.

Could someone help me with using a custom user agent for the headless chrome? I can't seem to get over the escaping problem. Thanks.

Bashan answered 1/2, 2019 at 8:51 Comment(0)
B
6

I actually found the answer, you need to escape with \\ every ; character.

E.g:

chrome:headless:userAgent=Mozilla/5.0 (X11\\; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36

will work.

In case of using in cli command you need to double escape. (I didn't have success in that)

Bashan answered 1/2, 2019 at 9:23 Comment(1)
I am facing similar issue. I am setting chromeargs : = ["--headless", "--disable-gpu", "--window-size=1920,1080", "--no-sandbox", "--disable-dev-shm-usage", "--disable-extensions", "--start-maximized", '--user-agent=Mozilla/5.0 (X11\\; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36']; url is still blockedStuppy

© 2022 - 2024 — McMap. All rights reserved.