Chrome 79 之后:
chrome_options.add_experimental_option('excludeSwitches',['enable-automation'])
这句代码已经失效,window.navigator.webdriver的值一直是true
可以通过以下方式解决:
option= ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
driver = Chrome(executable_path=driver_path,options=option)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
driver.execute_cdp_cmd("Network.enable", {})
driver.execute_cdp_cmd("Network.setExtraHTTPHeaders", {"headers": {"User-Agent": "browser1"}})
这种方式在刷新页面之后window.navigator.webdriver的值依然是undefined