110

I'm trying to automate a very basic task in a website using selenium and chrome but somehow the website detects when chrome is driven by selenium and blocks every request. I suspect that the website is relying on an exposed DOM variable like this one https://stackoverflow.com/a/41904453/648236 to detect selenium driven browser.

My question is, is there a way I can make the navigator.webdriver flag false? I am willing to go so far as to try and recompile the selenium source after making modifications, but I cannot seem to find the NavigatorAutomationInformation source anywhere in the repository https://github.com/SeleniumHQ/selenium

Any help is much appreciated

P.S: I also tried the following from https://w3c.github.io/webdriver/#interface

Object.defineProperty(navigator, 'webdriver', {
    get: () => false,
  });

But it only updates the property after the initial page load. I think the site detects the variable before my script is executed.

0

16 Answers 16

162

First the update 1

execute_cdp_cmd(): With the availability of execute_cdp_cmd(cmd, cmd_args) command now you can easily execute commands using Selenium. Using this feature you can modify the navigator.webdriver easily to prevent Selenium from getting detected.


Preventing Detection 2

To prevent Selenium driven WebDriver getting detected a niche approach would include either / all of the below mentioned steps:

  • Adding the argument --disable-blink-features=AutomationControlled

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument('--disable-blink-features=AutomationControlled')
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.website.com")
    

You can find a relevant detailed discussion in Selenium can't open a second page

  • Rotating the through execute_cdp_cmd() command as follows:

    #Setting up Chrome/83.0.4103.53 as useragent
    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
    
  • Change the property value of the navigator for webdriver to undefined

    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    
  • Exclude the collection of enable-automation switches

    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    
  • Turn-off useAutomationExtension

    options.add_experimental_option('useAutomationExtension', False)
    

Sample Code 3

Clubbing up all the steps mentioned above and effective code block will be:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
print(driver.execute_script("return navigator.userAgent;"))
driver.get('https://www.httpbin.org/headers')

History

As per the W3C Editor's Draft the current implementation strictly mentions:

The webdriver-active flag is set to true when the user agent is under remote control which is initially set to false.

Further,

Navigator includes NavigatorAutomationInformation;

It is to be noted that:

The NavigatorAutomationInformation interface should not be exposed on WorkerNavigator.

The NavigatorAutomationInformation interface is defined as:

interface mixin NavigatorAutomationInformation {
    readonly attribute boolean webdriver;
};

which returns true if webdriver-active flag is set, false otherwise.

Finally, the navigator.webdriver defines a standard way for co-operating user agents to inform the document that it is controlled by WebDriver, so that alternate code paths can be triggered during automation.

Caution: Altering/tweaking the above mentioned parameters may block the navigation and get the WebDriver instance detected.


Update (6-Nov-2019)

As of the current implementation an ideal way to access a web page without getting detected would be to use the ChromeOptions() class to add a couple of arguments to:

  • Exclude the collection of enable-automation switches
  • Turn-off useAutomationExtension

through an instance of ChromeOptions as follows:

  • Java Example:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver =  new ChromeDriver(options);
    driver.get("https://www.google.com/");
    
  • Python Example

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get("https://www.google.com/")
    
  • Ruby Example

      options = Selenium::WebDriver::Chrome::Options.new
      options.add_argument("--disable-blink-features=AutomationControlled")
      driver = Selenium::WebDriver.for :chrome, options: options
    

Legends

1: Applies to Selenium's Python clients only.

2: Applies to Selenium's Python clients only.

3: Applies to Selenium's Python clients only.

19
  • 5
    so how do we do this? Iam sorry i dint get the point. Iam using python client.
    – Ja8zyjits
    Commented Apr 11, 2019 at 10:39
  • 4
    @DebanjanB Should I also use --disable-blink-features and --disable-blink-features=AutomationControlled?
    – F. Vosnim
    Commented May 19, 2020 at 20:30
  • 3
    navigator.webdriver is still true when it comes to open a new page
    – jhd
    Commented Jul 3, 2020 at 8:03
  • 4
    This is not working anymore. They patched the switches "enable-automation" and "useAutomationExtension" and they are now behaving correctly, showing navigator.wedriver as true. Furthermore Im failry sure that this part never worked: driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    – aydunno
    Commented Aug 24, 2020 at 19:41
  • 5
    On January 21, none of these techniques work anymore. I have the feeling everything stopped working after a chrome update. Commented Jan 22, 2021 at 22:22
81

ChromeDriver:

Finally discovered the simple solution for this with a simple flag! :)

--disable-blink-features=AutomationControlled

navigator.webdriver=true will no longer show up with that flag set.

For a list of things you can disable, check them out here

11
  • 5
    OMG!! It worked for me in chromedriver 83. I tried a lot of solutions but this was the only one that worked perfectly. I added with python using chromeOptions.add_argument. Thanks
    – rodrigorf
    Commented Jun 21, 2020 at 22:58
  • 2
    Absolutely brilliant! ... for anyone using symfony/panther with chrome you can achieve this by setting the PANTHER_CHROME_ARGUMENTS="--disable-dev-shm-usage --disable-blink-features=AutomationControlled --window-size=1920x1080" Commented Aug 12, 2020 at 8:30
  • 12
    Thanks so much!! The code in python: options.add_argument("--disable-blink-features=AutomationControlled") Commented Sep 12, 2020 at 10:47
  • 1
    Hi can anyone send me exact line of code to avoid the detection? Thanks Commented Oct 1, 2020 at 11:14
  • 3
    worked very well. but now headless doesn't work. Any help ? Commented Oct 29, 2020 at 0:32
34

Do not use cdp command to change webdriver value as it will lead to inconsistency which later can be used to detect webdriver. Use the below code, this will remove any traces of webdriver.

options.add_argument("--disable-blink-features=AutomationControlled")
3
  • 1
    I use Chrome 88.0.4324.150 , is work fine. thanks.
    – huagang
    Commented Feb 8, 2021 at 8:29
  • @Baki Billah could you help me do the sameon webdriverio. I tried adding args: ['enable-automation', '--disable-blink-features=AutomationControlled', '--disable-blink-features'] and didn't help.
    – ChanChow
    Commented Oct 1, 2021 at 4:27
  • 1
    Chrome 116.0.5845.9600, work fine
    – Jim
    Commented Aug 24, 2023 at 11:20
24

Before (in browser console window):

> navigator.webdriver
true

Change (in selenium):

// C#
var options = new ChromeOptions();
options.AddExcludedArguments(new List<string>() { "enable-automation" });

// Python
options.add_experimental_option("excludeSwitches", ['enable-automation'])

After (in browser console window):

> navigator.webdriver
undefined

This will not work for version ChromeDriver 79.0.3945.16 and above. See the release notes here

6
  • 4
    How do to the same in Firefox? stackoverflow.com/questions/57122151/…
    – yegor256
    Commented Aug 23, 2019 at 5:25
  • 1
    I can also confirm this--to my eye--no longer works. Doesn't successfully change navigator.webdriver. Would love alternatives.
    – Rishi
    Commented Oct 31, 2019 at 18:48
  • Worked for me, the variable has been erased.
    – XavierAM
    Commented Nov 1, 2019 at 11:11
  • Worked for me too. Chrome 78.0.3904.97, with chromedriver 78.0.3904.10500.
    – deerchao
    Commented Nov 21, 2019 at 3:34
  • 1
    The C# version of this worked for me. driver 85.0.4183.87
    – Roro
    Commented Sep 25, 2020 at 15:48
18

To exclude the collection of enable-automation switches as mentioned in the 6-Nov-2019 update of the top voted answer doesn't work anymore as of April 2020. Instead I was getting the following error:

ERROR:broker_win.cc(55)] Error reading broker pipe: The pipe has been ended. (0x6D)

Here's what's working as of 6th April 2020 with Chrome 80.

Before (in the Chrome console window):

> navigator.webdriver
true

Python example:

options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")

After (in the Chrome console window):

> navigator.webdriver
undefined
3
  • Tried a ton of things but these two worked for me! Chromium ver 85 Thanks a lot!!!
    – Serge
    Commented Oct 13, 2020 at 18:37
  • @serge, same here. Working as of Chrome v97
    – jf328
    Commented Feb 16, 2022 at 3:27
  • Came here to say this works for me as well in Chrome v111! Commented Mar 14, 2023 at 4:04
16

Nowadays you can accomplish this with cdp command:

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})

driver.get(some_url)

by the way, you want to return undefined, false is a dead giveaway.

1
  • 4
    nowadays (i.e. Chrome 97.0.4692.71) it's actually false. Can't comment for whether false/undefined for Chrome back in '19 though surprised if it's changed since (?)
    – JB-007
    Commented Jan 14, 2022 at 23:28
15

Since this question is related to selenium a cross-browser solution to overriding navigator.webdriver is useful. This could be done by patching browser environment before any JS of target page runs, but unfortunately no other browsers except chromium allows one to evaluate arbitrary JavaScript code after document load and before any other JS runs (firefox is close with Remote Protocol).

Before patching we needed to check how the default browser environment looks like. Before changing a property we can see it's default definition with Object.getOwnPropertyDescriptor()

Object.getOwnPropertyDescriptor(navigator, 'webdriver');
// undefined

So with this quick test we can see webdriver property is not defined in navigator. It's actually defined in Navigator.prototype:

Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver');
// {set: undefined, enumerable: true, configurable: true, get: ƒ}

It's highly important to change the property on the object that owns it, otherwise the following can happen:

navigator.webdriver; // true if webdriver controlled, false otherwise
// this lazy patch is commonly found on the internet, it does not even set the right value
Object.defineProperty(navigator, 'webdriver', {
  get: () => undefined
});
navigator.webdriver; // undefined
Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get.apply(navigator);
// true

A less naive patch would first target the right object and use right property definition, but digging deeper we can find more inconsistences:

const defaultGetter = Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get;
defaultGetter.toString();
// "function get webdriver() { [native code] }"
Object.defineProperty(Navigator.prototype, 'webdriver', {
  set: undefined,
  enumerable: true,
  configurable: true,
  get: () => false
});
const patchedGetter = Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get;
patchedGetter.toString();
// "() => false"

A perfect patch leaves no traces, instead of replacing getter function it would be good if we could just intercept the call to it and change the returned value. JavaScript has native support for that throught Proxy apply handler:

const defaultGetter = Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get;
defaultGetter.apply(navigator); // true
defaultGetter.toString();
// "function get webdriver() { [native code] }"
Object.defineProperty(Navigator.prototype, 'webdriver', {
  set: undefined,
  enumerable: true,
  configurable: true,
  get: new Proxy(defaultGetter, { apply: (target, thisArg, args) => {
    // emulate getter call validation
    Reflect.apply(target, thisArg, args);
    return false;
  }})
});
const patchedGetter = Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get;
patchedGetter.apply(navigator); // false
patchedGetter.toString();
// "function () { [native code] }"

The only inconsistence now is in the function name, unfortunately there is no way to override the function name shown in native toString() representation. But even so it can pass generic regular expressions that searches for spoofed browser native functions by looking for { [native code] } at the end of its string representation. To remove this inconsistence you can patch Function.prototype.toString and make it return valid native string representations for all native functions you patched.

To sum up, in selenium it could be applied with:

chrome.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': """
    Object.defineProperty(Navigator.prototype, 'webdriver', {
        set: undefined,
        enumerable: true,
        configurable: true,
        get: new Proxy(
            Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get,
            { apply: (target, thisArg, args) => {
                // emulate getter call validation
                Reflect.apply(target, thisArg, args);
                return false;
            }}
        )
    });
"""})

The playwright project maintains a fork of Firefox and WebKit to add features for browser automation, one of them is equivalent to Page.addScriptToEvaluateOnNewDocument, but there is no implementation for Python of the communication protocol but it could be implemented from scratch.

4
  • Do you know where can I get this information for Python, ChromeRemotedriver, Selenium4?
    – The Dan
    Commented Oct 27, 2021 at 18:30
  • @TheDan the code above is for python, in selenium 4 you need to get the CDP session and call execute_cdp_cmd. You should ask a new question for that.
    – hldev
    Commented Oct 28, 2021 at 21:50
  • Appreciate the in depth answer. Page.add_init_script seems well suited for playwright-python Commented Dec 9, 2022 at 3:43
  • is it still not possible to override function name? I want to make it 100% match "function get webdriver() { [native code] }" Also, in descriptor it is show as "Proxy (Function)"
    – PATAPOsha
    Commented May 21 at 12:21
10

Finally this solved the problem for ChromeDriver, Chrome greater than v79.

Java:

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-blink-features");
options.addArguments("--disable-blink-features=AutomationControlled");
ChromeDriver driver = new ChromeDriver(options);
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);

Python:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-blink-features")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

driver = webdriver.Chrome(options=chrome_options)

params = {
    "source": "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })"
}

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", params)
1
  • 2
    Can you share this in python ? Commented Oct 1, 2020 at 11:37
7

Simple hack for python:

options = webdriver.ChromeOptions()    
options.add_argument("--disable-blink-features=AutomationControlled")
3

I would like to add a Java alternative to the cdp command method mentioned by pguardiario

Map<String, Object> params = new HashMap<String, Object>();
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);

In order for this to work you need to use the ChromiumDriver from the org.openqa.selenium.chromium.ChromiumDriver package. From what I can tell that package is not included in Selenium 3.141.59 so I used the Selenium 4 alpha.

Also, the excludeSwitches/useAutomationExtension experimental options do not seem to work for me anymore with ChromeDriver 79 and Chrome 79.

3

As mentioned in the above comment - https://stackoverflow.com/a/60403652/2923098 the following option totally worked for me (in Java)-

ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito", "--disable-blink-features=AutomationControlled");
3

For those of you who've tried these tricks, please make sure to also check that the user-agent that you are using is the user agent that corresponds to the platform (mobile / desktop / tablet) your crawler is meant to emulate. It took me a while to realize that was my Achilles heel ;)

1
  • 3
    This should be a comment.
    – Robino
    Commented Mar 27, 2021 at 20:39
3

Python

I tried most of the stuff mentioned in this post and i was still facing issues. What saved me for now is https://pypi.org/project/undetected-chromedriver

pip install undetected-chromedriver


import undetected_chromedriver.v2 as uc
from time import sleep
from random import randint


driver = uc.Chrome()
driver.get('www.your_url.here')
driver.maximize_window() 

sleep(randint(3,9))

A bit slow but i will take slow over non working.

I guess if every interested could go over the source code and see what provides the win there.

1
  • 1
    Thanks. I also tried much of the solutions up there and didn't work. Now with undetected-chromedriver, it works.
    – kitokid
    Commented May 19, 2022 at 5:08
2

Use --disable-blink-features=AutomationControlled to disable navigator.webdriver

1

If you use a Remote Webdriver , the code below will set navigator.webdriver to undefined.

work for ChromeDriver 81.0.4044.122

Python example:

    options = webdriver.ChromeOptions()
    # options.add_argument("--headless")
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    driver = webdriver.Remote(
       'localhost:9515', desired_capabilities=options.to_capabilities())
    script = '''
    Object.defineProperty(navigator, 'webdriver', {
        get: () => undefined
    })
    '''
    driver.execute_script(script)
0

"AutomationControlled" was used to prevent detection in old version of headless mode by some websites:

  chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
  chromeOptions.addArguments("--headless")

But not supported on New Version of headless:

chromeOptions.addArguments("--headless=new");

In my case i used:

String userAgent = (String) ((JavascriptExecutor) initialDriver).executeScript("return navigator.userAgent;");

and

String modifiedUserAgent = userAgent.replace("HeadlessChrome", "Chrome");

Not the answer you're looking for? Browse other questions tagged or ask your own question.