Selenium(一)——移除window.navigator.webdriver特征值
一、通过运行js覆盖隐藏掉特征值(已经失效了)
失效原因:
execute_script方法是在页面加载完成之后才调用运行的js语句的,而网站早在渲染之前就已经检测webdriver的属性了。
from selenium import webdriver
driver = webdriver.Chrome(executable_path='webdriver路径')
driver.execute_script("""Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})""")
driver.get(网站地址)
二、用Chrome开发者工具(CDP)(部分有效)
用开发者工具,在运行加载页面之前先改
option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option,executable_path=‘’)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{
'source':'''Object.defineProperty(navigator, 'webdriver', {
get: () => undefined}'''
})
driver.get(网站地址)
三、运行一个js文件
driver = webdriver.Chrome(executable_path=executable_path)
with open('stealth.min.js') as f:
js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
driver.get(网址)
stealth.min.js:
https://github.com/kingname/stealth.min.js
四、在方法三中,多加一个条件
option.add_argument("–disable-blink-features=AutomationControlled")
option = webdriver.ChromeOptions()
option.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(executable_path=executable_path)
with open('stealth.min.js') as f:
js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
driver.get(网址)