已解决(selenium 操作火狐Firefox浏览器报错)AttributeError: ‘WebDriver’ object has no attribute ‘execute_cdp_cmd’

已解决(selenium 操作火狐Firefox浏览器使用 stealth.min.js文件隐藏浏览器指纹特征报错)AttributeError: ‘WebDriver’ object has no attribute ‘execute_cdp_cmd’







报错代码


我的代码如下,想用stealth.min.js文件隐藏浏览器指纹特征,之前用Chrome浏览器都没有问题:

from selenium import webdriver

# 1. 初始化配置对象
options = webdriver.FirefoxOptions()
# 2. 无界面模式
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# 3. 添加请求头伪装浏览器
options.add_argument(
    'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0')
# 4. 告诉浏览器去掉了webdriver痕迹
options.add_argument("--disable-blink-features=AutomationControlled")
# 5. 不加载图片提高访问速度
options.add_argument('blink-settings=imagesEnabled=false')
options.add_argument('--disable-images')
driver = webdriver.Firefox(options=options)
# 6. 隐式等待10秒
driver.implicitly_wait(10)
# 7. 隐藏浏览器指纹
with open('stealth.min.js') as f:
    js = f.read()

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    
    
    "source": js
})

driver.get('https://bot.sannysoft.com/')
driver.save_screenshot('1.png')

driver.quit()

报错信息

Traceback (most recent call last):
  File "E:/Python学习/1.py", line 23, in <module>
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    
    
AttributeError: 'WebDriver' object has no attribute 'execute_cdp_cmd'

在这里插入图片描述



报错翻译


报错内容翻译:属性错误 :“WebDriver”对象没有属性“execute\u cdp\u cmd”,及没有那个方法无法使用



报错原因


报错原因:cdp即Chrome DevTools Protocal, Chrome开发者工具协议,只适用于 Chrome浏览器,其他浏览器并不能使用,但我试过Edge浏览器也可以用



解决方法


1. 去掉报错代码不隐藏浏览器指纹


2. 切换成Chrome谷歌浏览器或者Edge浏览器使用stealth.min.js文件隐藏浏览器指纹特征

猜你喜欢

转载自blog.csdn.net/yuan2019035055/article/details/125842443