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

[NODRIVER] Bot detected in headless mode by user agent #2003

Open
FRIKIdelTO opened this issue Sep 2, 2024 · 4 comments
Open

[NODRIVER] Bot detected in headless mode by user agent #2003

FRIKIdelTO opened this issue Sep 2, 2024 · 4 comments

Comments

@FRIKIdelTO
Copy link

FRIKIdelTO commented Sep 2, 2024

nodriver is easily detectable in headless mode because the user agent is not patched:

browser.info.get("User-Agent")
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/127.0.0.0 Safari/537.36

How to fix this without setting a specific user agent in browser_args?

Also, I would like to know if there is any equivalent method to execute_script()

@devblack
Copy link

devblack commented Sep 2, 2024

Just override the user agent before open the page.

            await tab.send(cdp.network.set_user_agent_override(
                user_agent=browser.info.get("User-Agent").replace("Headless", "")
            ))

@FRIKIdelTO
Copy link
Author

Yes, but this would be at the tab level and I would like it to be at the browser level like with undetected_chromedriver, that is, if I open new tabs, the user agent would already be patched.

@devblack
Copy link

devblack commented Sep 2, 2024

Yes, but this would be at the tab level and I would like it to be at the browser level like with undetected_chromedriver, that is, if I open new tabs, the user agent would already be patched.

Use your own user agent as browser arg

@samuelvillegas
Copy link

Remove the "Headless" from de user agent has worked for me very well, to make it at browser level I added this line:

driver =  selenium.webdriver.chrome.webdriver.WebDriver(options=driver_opts)

current_user_agent = driver.execute_script("return navigator.userAgent;")
driver.execute_cdp_cmd(
    'Network.setUserAgentOverride',
    {
        "userAgent": current_user_agent.replace('Headless', ''),
    },
)

It worked doing int like this rather than adding the user agent argument

driver_opts.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')

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

3 participants