无法使用Selenium Webdriver。出现两个异常。

huangapple go评论268阅读模式
英文:

Unable to use Selenium Webdriver. Getting two exceptions

问题

尝试使用Selenium Webdriver创建对象时遇到以下错误。
"\selenium\webdriver\common\driver_finder.py",第42行,在get_path中
    path = SeleniumManager().driver_location(options) if path is None else path

"\selenium\webdriver\common\selenium_manager.py",第74行,在driver_location中
    browser = options.capabilities["browserName"]

属性错误:'str'对象没有属性'capabilities'

在处理上述异常期间,发生了另一个异常:

跟踪最近的调用(最近的调用在最上面)

"\selenium_webdriver_webscraping.py",第4行,在<module>中
    driver = webdriver.Chrome(chrome_driver_path)
"\selenium\webdriver\chrome\webdriver.py",第47行,在__init__中
    self.service.path = DriverFinder.get_path(self.service, self.options)

"\selenium\webdriver\common\driver_finder.py",第44行,在get_path中
    raise NoSuchDriverException(f"无法使用Selenium Manager获取{service.path};{err}")
selenium.common.exceptions.NoSuchDriverException:消息:使用Selenium Manager无法获取chromedriver;'str'对象没有属性'capabilities';有关此错误的文档,请访问:https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
这是我使用的代码:
from selenium import webdriver

chrome_driver_path = <chrome drive .exe路径>
driver = webdriver.Chrome(chrome_driver_path)
英文:

Getting the following error when trying to create object with Selenium Webdriver.

"\selenium\webdriver\common\driver_finder.py", line 42, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path

"\selenium\webdriver\common\selenium_manager.py", line 74, 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):
"\selenium_webdriver_webscraping.py", line 4, in <module>
    driver = webdriver.Chrome(chrome_driver_path)
"\selenium\webdriver\chrome\webdriver.py", line 47, in __init__
    self.service.path = DriverFinder.get_path(self.service, self.options)
"\selenium\webdriver\common\driver_finder.py", line 44, in get_path
    raise NoSuchDriverException(f"Unable to obtain {service.path} using Selenium Manager; {err}")
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager; 'str' object has no attribute 'capabilities'; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

This is the code I used:

from selenium import webdriver

chrome_driver_path = <chrome drive .exe path>
driver = webdriver.Chrome(chrome_driver_path)

答案1

得分: 20

如果你正在使用的Selenium版本是v4.6.0或更高版本(我认为是的,因为我在错误跟踪中看到了SeleniumManger),那么你实际上不需要设置driver.exe的路径。Selenium可以自己处理浏览器和驱动程序。

因此,你的代码可以简化如下:

from selenium import webdriver

driver = webdriver.Chrome()

一些参考链接:

英文:

If the selenium version you are using is v4.6.0 or above (which I think it is as I see SeleniumManger in the error trace), then you don't really have to set the driver.exe path. Selenium can handle the browser and drivers by itself.

So your code can be simplified as below:

from selenium import webdriver

driver = webdriver.Chrome()

Few references:

答案2

得分: 1

这是由于selenium 4.10.0的更改:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

无法使用Selenium Webdriver。出现两个异常。

请注意,第一个参数不再是executable_path,而且desired_capabilities已被移除,但现在有一种新的方法来传递它。有关在使用selenium 4.10.0(或更新版本)时如何传递所需的功能的文档,请参见https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/#capabilities。

另外,如果您想设置executable_path,可以通过service传递,但不再是必需的,因为selenium管理器已包含在内。

这是一个包含您所需的一切的代码片段:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service()
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
英文:

This is due to changes in selenium 4.10.0:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

无法使用Selenium Webdriver。出现两个异常。

Note that the first argument is no longer executable_path, and that desired_capabilities has been removed, but there is now another way of passing it in. See https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/#capabilities for the documentation on how to pass in desired capabilities when using selenium 4.10.0 (or newer).

Also, if you want to set an executable_path, it can be passed in via the service, but it is no longer necessary, as selenium manager is included.

Here's a code snippet with everything you need:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service()
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()

答案3

得分: -1

我遇到了下面相同的错误:

> AttributeError: 'str' object has no attribute 'capabilities'

这是因为我将 chromedriver.exe 的路径设置为 webdriver.Chrome() 如下所示:

from selenium import webdriver

driver = webdriver.Chrome('./chromedriver.exe')

所以,我从 webdriver.Chrome() 中移除了路径,如下所示,然后错误就解决了。*这是推荐的做法,你可以查看关于 我的问题 的答案,关于 webdriver.Chrome() 使用哪个版本的 Chrome 驱动程序:

from selenium import webdriver

driver = webdriver.Chrome()

或者,我将路径设置为 Service() 并将其传递给 webdriver.Chrome() 如下所示,然后错误就解决了:

from selenium.webdriver.chrome.service import Service
from selenium import webdriver

service = Service(executable_path='./chromedriver.exe')
driver = webdriver.Chrome(service=service)

另外,我遇到了下面相同的错误,因为我没有在 django-project 中下载和设置 chromedriver.exe

> selenium.common.exceptions.NoSuchDriverException: Message: Unable to locate or obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

这是我的代码:

# "tests/test_1.py"

from django.test import LiveServerTestCase
from selenium.webdriver.chrome.service import Service
from selenium import webdriver

class TestBrowser1(LiveServerTestCase):
    def test_example(self):
        service = Service(executable_path='./chromedriver')
        driver = webdriver.Chrome(service=service)
        driver.get('%s%s' % (self.live_server_url, "/admin/"))
        assert "Log in | Django site admin" in driver.title

所以,我下载了 chromedriver.exe 并将其设置到 django-project 根目录中,如下所示,然后错误就解决了:

django-project
 |-core
 |  |-settings.py
 |  └-urls.py
 |-my_app1
 |-my_app2
 |-tests
 |  |-__init__.py
 |  └-test_1.py
 └-chromedriver.exe # 这里
英文:

I got the same error below:

> AttributeError: 'str' object has no attribute 'capabilities'

Because I set the path of chromedriver.exe to webdriver.Chrome() as shown below:

from selenium import webdriver

driver = webdriver.Chrome('./chromedriver.exe')

So, I removed the path from webdriver.Chrome() as shown below, then the error was solved. *This is recommended and you can see the answers of my question about which version of chrome driver webdriver.Chrome() gets:

from selenium import webdriver

driver = webdriver.Chrome()

Or, I set the path to Service() and set it to webdriver.Chrome() as shown below, then the error was solved:

from selenium.webdriver.chrome.service import Service
from selenium import webdriver

service = Service(executable_path='./chromedriver.exe')
driver = webdriver.Chrome(service=service)

And, I got the same error below because I did not download and set chromedriver.exe in django-project:

> selenium.common.exceptions.NoSuchDriverException: Message: Unable to locate or obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

This is my code:

# "tests/test_1.py"

from django.test import LiveServerTestCase
from selenium.webdriver.chrome.service import Service
from selenium import webdriver

class TestBrowser1(LiveServerTestCase):
    def test_example(self):
        service = Service(executable_path='./chromedriver')
        driver = webdriver.Chrome(service=service)
        driver.get(("%s%s" % (self.live_server_url, "/admin/")))
        assert "Log in | Django site admin" in driver.title

So, I downloaded chromedriver.exe and set it to the root directory in django-project as shown below, then the error was solved:

django-project
 |-core
 |  |-settings.py
 |  └-urls.py
 |-my_app1
 |-my_app2
 |-tests
 |  |-__init__.py
 |  └-test_1.py
 └-chromedriver.exe # Here

huangapple
  • 本文由 发表于 2023年6月13日 11:52:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定