已解决(selenium操作火狐浏览器报错)TypeError: init() got an unexpected keyword argument ‘firefox_options‘
报错代码
我的代码:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# 1. 初始化配置对象
options = Options()
# 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(firefox_options=options)
# 6. 隐式等待10秒
driver.implicitly_wait(10)
driver.get('https://bot.sannysoft.com/')
driver.save_screenshot('2.png')
driver.quit()
报错信息:
Traceback (most recent call last):
File "E:/Python/test5.py", line 17, in <module>
driver = webdriver.Firefox(firefox_options=options)
TypeError: __init__() got an unexpected keyword argument 'firefox_options'
报错翻译
报错内容翻译:类型错误:获得意外的关键字参数“firefox\u options”
报错原因
报错原因:
Selenium 3.8中不推荐使用浏览器选项参数firefox_options。
浏览器选项参数现在在驱动程序中标准化为options
。firefox_options、chrome_options和ie_options现在不推荐使用
解决方法
修改代码:
扫描二维码关注公众号,回复:
14385396 查看本文章
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# 1. 初始化配置对象
options = Options()
# 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)
driver.get('https://bot.sannysoft.com/')
driver.save_screenshot('2.png')
driver.quit()
高亮的报错参数消失了: