Python爬虫中selenium的一个小问题

今天跟随B站老师学习selenium的基本使用出现一个错误。代码如下:

#导入selenium
from selenium import webdriver

#创建浏览器操作对象
path = 'msedgedriver.exe'
browser = webdriver.Edge(path)

#访问网站
url = 'https://www.baidu.com'
browser.get(url)

显然,我用的是Edge浏览器,检查了驱动器版本没有问题。运行报错如下:

D:\python\python.exe D:/pyexample/demo/selenium基本使用.py
Traceback (most recent call last):
  File "D:\python\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
  File "D:\python\lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 87, in driver_location
    browser = options.capabilities["browserName"]
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/pyexample/demo/selenium基本使用.py", line 6, in <module>
    browser = webdriver.Edge(path)
  File "D:\python\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__
    super().__init__(
  File "D:\python\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
  File "D:\python\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path
    msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
AttributeError: 'str' object has no attribute 'capabilities'

在网上寻求多种办法,都没有解决,后来将browser = webdriver.Edge(path)中的path删除,代码如下:

#导入selenium
from selenium import webdriver

#创建浏览器操作对象
path = 'msedgedriver.exe'
browser = webdriver.Edge()

#访问网站
url = 'https://www.baidu.com'
browser.get(url)

发现能够正常运行。

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值