English 中文(简体)
无法使用Selenium Webdriver。 有两个例外
原标题:Unable to use Selenium Webdriver. Getting two exceptions

在试图与Selenium Webdriver制造一个物体时,我犯了以下错误。

"seleniumwebdrivercommondriver_finder.py", line 42, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path

"seleniumwebdrivercommonselenium_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)
"seleniumwebdriverchromewebdriver.py", line 47, in __init__
    self.service.path = DriverFinder.get_path(self.service, self.options)
"seleniumwebdrivercommondriver_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)
最佳回答

如果您使用的Selenium版本为v4.6.0或以上(我认为,正如我所看到的那样,它出现在错误的痕迹中,那么你实际上不必设置<代码>driver.exe。 ium可以自行处理浏览器和司机。

因此,你的法典可以简化如下:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com/")
driver.quit()

几个参考资料:

问题回答

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

“Changes_in_selenium_4_10_0

请注意,第一种论点已不再是<代码>可执行_path,而desired_capabilities已被删除,但目前还有另一种方式通过。 见Upward to Selenium 4>。

另外,如果你想要建立<条码>可执行->条码/代码>,可在<条码>服务上通过,但不再需要,因为包括ium管理人。

这里的法典载有你们所需要的一切:

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()

我的错误如下:

AttributeError:str客体没有特性

因为我把<代码>chromedriver.exe到webdriver.Chrome()的道路如下:

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()

或者,我确定了以下途径: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)

此外,我也得出同样的错误,因为我没有下载和定下chromedriver.exe,载于django-project:

se.common。 贺电:无法找到或取得用于幼ome的驾驶员; 关于这一错误的文件,请访问:

这是我的法典:

# "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




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...