安装和运行Selenium、Pycharm时出错

2024-09-28 05:15:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚安装了selenium,当我对selenium进行pip检查时,我看到了以下内容: qdarkstyle 2.8.1需要未安装的helpdev。 spyder 4.1.4要求pyqt5<;5.13; python_版本>;=“3”,但您有pyqt5 5.15.1。 spyder 4.1.4要求pyqtwebengine<;5.13; python_版本>;=“3”,但您有pyqtwebengine 5.15.1

当我尝试在Pycharm中运行基本程序(如下所示)时,我看到异常:

import time
from selenium import webdriver

driver = webdriver.Chrome("../Drivers/chromedriver.exe")

driver.set_page_load_timeout(10)

driver.get("http://google.com")

driver.find_element_by_name("q").send_keys("Automation step by step")

driver.find_element_by_name("btnK").click()

time.sleep(2)

driver.close()

driver.quit()


print("Test")

--------------------
"/Applications/Python 3.8/IDLE.app/Contents/MacOS/Python" /Users/Anu/PycharmProjects/pythonProject3/Demo/test1.py
Traceback (most recent call last):
  File "/Users/Anu/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/common/service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '../Drivers/chromedriver.exe'

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

Traceback (most recent call last):
  File "/Users/Anu/PycharmProjects/pythonProject3/Demo/test1.py", line 4, in <module>
    driver = webdriver.Chrome("../Drivers/chromedriver.exe")
  File "/Users/Anu/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/Users/Anu/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/common/service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Tags: inpyselflibdriverseleniumlinelibrary
2条回答

您的错误可能与您的Chrome驱动程序有关。您可以使用webdriver-manager 要安装此模块,请使用pip install webdriver-manager并将此行粘贴到脚本中

from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install())

问题是您向ChromeDriver传递了错误的路径,请尝试将ChromeDriver放在项目目录中。 另外,据我所知,您正在Windows操作系统上运行,您需要将ChromeDriver添加为sys PATH变量上的exe文件

https://developers.refinitiv.com/sites/default/files/How%20To%20Add%20ChromeDriver%20To%20System%20Variables_0.pdf

相关问题 更多 >

    热门问题