pycharm 尝试使用Python Selenium进行测试-控制台中出现错误

f45qwnt8  于 2023-10-20  发布在  PyCharm
关注(0)|答案(2)|浏览(195)

我是初学者,所以我不明白这种类型的错误的原因:

Traceback (most recent call last):
  File "C:\Users\kerem\PycharmProjects\pythonProject\main.py", line 5, in <module>
    service = Service(executable_path=ChromeDriverManager().install())
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kerem\PycharmProjects\pythonProject\venv\Lib\site-packages\webdriver_manager\chrome.py", line 40, in install
    driver_path = self._get_driver_binary_path(self.driver)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kerem\PycharmProjects\pythonProject\venv\Lib\site-packages\webdriver_manager\core\manager.py", line 40, in _get_driver_binary_path
    file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kerem\PycharmProjects\pythonProject\venv\Lib\site-packages\webdriver_manager\core\download_manager.py", line 29, in download_file
    response = self._http_client.get(url)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kerem\PycharmProjects\pythonProject\venv\Lib\site-packages\webdriver_manager\core\http.py", line 36, in get
    self.validate_response(resp)
  File "C:\Users\kerem\PycharmProjects\pythonProject\venv\Lib\site-packages\webdriver_manager\core\http.py", line 19, in validate_response
    raise ValueError(
ValueError: response body:
Google-Edge-Cache: forbidden
Error: 7
request url:
https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/win32/chromedriver-win32.zip
response headers:
{'content-length': '37', 'content-type': 'text/plain', 'x-request-id': 'ca594d4d-aa62-4ebf-8c4c-04a30cc6a844', 'alt-svc': 'h3=":443"; ma=2592000, h3-29=":443"; ma=2592000', 'date': 'Tue, 05 Sep 2023 16:00:53 GMT', 'server': 'Google-Edge-Cache'}

Process finished with exit code 1

我试着运行这个代码

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

service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
e3bfsja2

e3bfsja21#

您使用的是最新版本的selenium,您不需要使用第三方库(如ChromeDriverManager)来管理浏览器驱动程序。代码可以像这样简单:

from selenium import webdriver

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

参考此答案-https://stackoverflow.com/a/76463081/7598774

sbdsn5lh

sbdsn5lh2#

由于您是使用Selenium的新手,我假设您下载了Selenium的最新版本。尽管如此,您正在使用一段过时的代码来启动Web驱动程序。
在新版本的Selenium中,这段代码与此类似:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.python.org")

阅读更多Selenium文档:链路

相关问题