In Skill in a Day: How to remove the Selenium in the window. The navigator. Webdriver the value of the article, we introduced at the time properly removed from the start the Selenium Chrome window. The navigator. Webdriver method.

Later, Chrome updated its version, rendering the method ineffective. As shown below:

How do we properly hide this parameter for the latest version of Chrome?

In that article, I cursed a way of covering my ears:

Open the web page, and then by performing the following JavaScript statements to hide the window. The navigator. Webdriver value:

Object.defineProperty(navigator, 'webdriver', {
      get: (a)= > undefined
    })
Copy the code

I scolded the way as the ostrich, because they are in the web page is loaded after running this JavaScript code, can be as the site’s own js to early by reading window. The navigator. Webdriver know you now use the simulated browser, What good does it do you to hide it?

So if you do execute this JavaScript statement, it should be before the browser runs all of the JavaScript that comes with the site.

So that’s what we’re doing right now.

Some readers may think that it is not possible to write a Chrome browser plug-in, so that the JavaScript statement inside the plug-in just opened the site page, before running the native JavaScript.

This way can solve the problem, but a little bit of trouble, we today’s method is very simple. It uses Google’s Chrome Devtools-Protocol (CDP) for short.

When we open the official CPD documentation, we can see the following command:

Run the given script just after each Frame is opened and before the Frame script is run.

With this command, we can give Chrome a piece of JavaScript code to execute as soon as it opens each page and does not run the site’s native JavaScript code.

How do I invoke CDP’s commands in Selenium? It’s actually quite simple, we use driver.execute_cdp_cmd. According to Selenium’s official documentation, just pass in the CDP command and parameters to invoke:

So we can write the following code:

from selenium.webdriver import Chrome

driver = Chrome('./chromedriver')
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """
})
driver.get('http://exercise.kingname.info')
Copy the code

The operating effect is shown in the figure below:

Perfect hidden window. The navigator. Webdriver. And, key statements:

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

Need to be performed only once, as long as you don’t shut down after the driver opened the window, no matter how much you open a website, he automatically in advance before site bringing all js execute the statement, hidden window. The navigator. Webdriver.

If someone runs the above code, the following error occurs:

Upgrade your ChromeDriver. Older versions of Chrome + ChromeDriver can only use the old method, not today’s method. The new version of Chrome + ChromeDriver can use today’s methods, but not the old ones. Exactly the sentence:

When God closes a door for you, he quietly opens a window for you.

The above code will do the trick, but for better hiding, you can also add two experimental options:

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='./chromedriver')
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """
})
driver.get('http://exercise.kingname.info')
Copy the code