我刚刚安装了selenium,当我对selenium进行pip检查时,我看到了以下内容:qdarkstyle 2.8.1需要未安装的helpdev。spyder 4.1.4具有要求pyqt5<5.13;python_version>="3〃;,但你有第5.15.1节。spyder 4.1.4具有pyqtwebengine<5.13;python_version>="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
问题是,您将错误的路径传递到ChromeDriver,请尝试将ChromeDrive放在Project目录中。此外,据我所知,您在Windows操作系统上运行,您需要将ChromeDriver添加为sys PATH变量上的exe文件。
https://developers.refinitiv.com/sites/default/files/How%20To%20Add%20ChromeDriver%20To%20System%20Variables_0.pdf
您的错误可能与Chrome驱动程序有关。可以使用webdriver-manager
要安装此模块,请使用pip install webdriver-manager
并将此行粘贴到您的脚本
from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install())