0


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)

发现能够正常运行。

标签: python 爬虫 selenium

本文转载自: https://blog.csdn.net/ryanies/article/details/136001773
版权归原作者 ryanies 所有, 如有侵权,请联系我们删除。

“Python爬虫中selenium的一个小问题”的评论:

还没有评论