Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to correctly remove the

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to correctly remove the Selenium. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Here's how to correctly remove window.navigator.webdriver from a Chrome browser launched by Selenium.

Then things changed and the version of Chrome was upgraded, so that the method at that time had become invalid. As shown in the following figure:

How should we correctly hide this parameter for the latest version of Chrome?

In that article, I scolded a way to cover my ears and steal the bell:

Open the web page and hide the value of window.navigator.webdriver by executing the following JavaScript statement:

Object.defineProperty (navigator, 'webdriver', {

Get: () = > undefined

})

I scolded this method as a cover-up because they didn't run this JavaScript code until the page had been loaded, but at this time the site's own js program already knew by reading window.navigator.webdriver that you are now using an analog browser, so what's the use of hiding it?

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

This is our current plan.

Some readers may think that by writing a plug-in for Chrome browser, the JavaScript statement in the plug-in can be run just before the website page is opened and before the built-in JavaScript is run.

Although this approach can solve the problem, it is a little troublesome. Our method today is very simple. Is the use of Google's Chrome Devtools-Protocol (Chrome Development tools Protocol) referred to as CDP.

When we open the official document [1] of CPD, we can see the following command:

Run the given script before each Frame has just been opened and the script for Frame has not been run.

"

With this command, we can give a piece of JavaScript code and let Chrome execute the given code as soon as it opens each page and doesn't run the JavaScript code that comes with the site.

So how do you invoke CDP commands in Selenium? It's actually very simple, we use driver.execute_cdp_cmd. According to the official document of Selenium [2], you can pass in the CDP commands and parameters that need to be called:

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')

The running effect is shown in the following figure:

Perfectly hide window.navigator.webdriver. And, the key statement:

Driver.execute_cdp_cmd ("Page.addScriptToEvaluateOnNewDocument", {

Source ":"

Object.defineProperty (navigator, 'webdriver', {

Get: () = > undefined

})

"

})

It only needs to be executed once, and then as long as you don't close the window that driver opens, no matter how many URLs you open, it will automatically execute this statement in advance before all the js that comes with the site, hiding the window.navigator.webdriver.

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

Then please upgrade your ChromeDriver. The old version of Chrome + ChromeDriver can only use the previous method, not today's method. The new version of Chrome + ChromeDriver can use today's method, but not the old one. That's exactly what he said:

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

"

Although you can achieve this with the above code, in order to achieve a better hiding effect, you can continue to 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') above is what Xiaobian shared with you about how to remove Selenium correctly. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report