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(网址)
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐