无法运行我的第一个 Selenium 和 Python 测试

问题描述 投票:0回答:1

我似乎缺少一些配置才能正确运行测试。 我遵循的步骤: -在一个新文件夹中,我创建一个“main.py”文件。 -使用“python3 -m venv .venv”命令通过终端创建一个新的虚拟环境 - 在我的 IDE 上出现“我们注意到已创建新环境。您想使用它吗”消息时单击“是” -通过运行“python3 main.py”命令杀死终端并执行以下代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

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

driver.get("https://www.python.org")
print(driver.title)

我收到以下错误消息:

 % python main.py
Traceback (most recent call last):
  File "/Users/xxxx/mechanized/.venv/lib/python3.11/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 "/Users/xxxx/mechanized/.venv/lib/python3.11/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 "/Users/xxxx/mechanized/main.py", line 4, in <module>
    driver = webdriver.Chrome('./chromedriver')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxx/mechanized/.venv/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/Users/xxxx/mechanized/.venv/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxx/mechanized/.venv/lib/python3.11/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'
python python-3.x selenium-webdriver
1个回答
0
投票

尝试 driver = webdriver.Chrome()。我认为你不需要在括号之间添加任何内容,因为我相信 Python,这意味着你正在传递一个 Selenium 可能无法理解的变量。

© www.soinside.com 2019 - 2024. All rights reserved.