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 remove the window.navigator.webdriver of Pyppeteer correctly

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

Share

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

How to correctly remove the window.navigator.webdriver of Pyppeteer, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Times have changed, and this approach has been invalidated with the upgrade of the Chrome version.

Today we'll talk about how to hide Pyppeteer.

Today's method is very simple and does not need to modify the source code.

If you read the Selenium version of the article, you should see that our principle is to execute a piece of JavaScript code through CDP. There is a keyword in this code called addScriptToEvaluateOnNewDocument. Means to add a script that is executed when a new document is opened.

Let's remember this keyword EvaluateOnNewDocument. Now open Pyppeteer's official API [1], and search evaluateOnNewDocument to see that Pyppeteer also supports this feature.

This makes the file very simple, so let's call it directly in Pyppeteer and pass the JavaScript code:

Import asyncio

From pyppeteer import launch

Async def main ():

Browser = await launch (executablePath='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', headless=False)

Page = await browser.newPage ()

Await page.evaluateOnNewDocument (''() = > {

Object.defineProperty (navigator, 'webdriver', {

Get: () = > undefined

})

}

'')

Await page.goto ('http://exercise.kingname.info')

Input ('press enter to close the window after checking.')

Await browser.close ()

Asyncio.run (main ())

The effect of the code is shown in the following figure:

Solve the problem perfectly. And as long as you do not open a new tab or window, only open a new URL or refresh the page in the current window, the js code takes effect automatically and does not need to be repeated.

The key code is:

Await page.evaluateOnNewDocument (''() = > {

Object.defineProperty (navigator, 'webdriver', {

Get: () = > undefined

})

}

'')

Let Pyppeteer execute the JavaScript function in the parameters before all the js that comes with the site executes when each new page is loaded.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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