关于python:selenium-chromedriver可执行文件必须位于PATH中

selenium - chromedriver executable needs to be in PATH

错误信息:

'chromedriver' executable needs to be in PATH

我试图在pycharm中使用Selenium编写脚本,但是发生了以上错误。 我已经将硒链接到pycharm,如此处所示(最新)。

我是硒的新手,不是" selenium"文件夹中的chromedriver。
如果不是,我在哪里可以找到它并将其添加到路径中?

顺便说一句,我尝试在cmd中键入" chromedriver",但是,它未被识别为内部或外部命令。

错误如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Traceback (most recent call last):
  File"C:\\Users\\sebastian\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\common\\service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File"C:\\Users\\sebastian\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File"C:\\Users\\sebastian\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\subprocess.py", line 1224, in _execute_child
    startupinfo)
PermissionError: [WinError 5] Permission denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File"C:/Users/sebastian/PycharmProjects/web/bot.py", line 10, in <module>
    browser = webdriver.Chrome("C:/Users/sebastian/desktop/selenium-3.0.1")
  File"C:\\Users\\sebastian\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\chrome\\webdriver.py", line 62, in __init__
    self.service.start()
  File"C:\\Users\\sebastian\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\common\\service.py", line 76, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'selenium-3.0.1' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x01EDEAF0>>
Traceback (most recent call last):
  File"C:\\Users\\sebastian\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\common\\service.py", line 163, in __del__
    self.stop()
  File"C:\\Users\\sebastian\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\common\\service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'


您可以在此处下载ChromeDriver:
https://sites.google.com/a/chromium.org/chromedriver/downloads

然后,您有多种选择:

  • 将其添加到您的系统path
  • 将其放在与Python脚本相同的目录中
  • 通过executable_path直接指定位置

    1
    driver = webdriver.Chrome(executable_path='C:/path/to/chromedriver.exe')


另一种方法是下载并解压缩chromedriver并将chromedriver.exe放入C:\ Python27 \ Scripts中,然后您无需提供驱动程序的路径,只需

1
driver= webdriver.Chrome()

将工作


尝试这个 :

1
2
3
4
5
pip install webdriver-manager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())


2020年的答案。以下代码解决了这个问题。许多硒新手似乎必须克服这一步骤。
安装chromedriver,并将其放在桌面上的文件夹中。还要确保将selenium python项目放在与chrome驱动程序所在的文件夹中。

根据您的计算机更改USER_NAME和FOLDER。

对于Windows

1
driver = webdriver.Chrome(r"C:\\Users\\USER_NAME\\Desktop\\FOLDER\\chromedriver")

对于Linux / Mac

1
driver = webdriver.Chrome("/home/USER_NAME/FOLDER/chromedriver")

不要在文件路径中包含" .exe"。

例如:

1
2
3
from selenium import webdriver

driver = webdriver.Chrome(executable_path='path/to/folder/chromedriver')

尝试这个 :

1
driver = webdriver.Chrome(ChromeDriverManager().install())

另一种方法是下载并解压缩chromedriver并将chromedriver.exe放入C:\ Python27 \ Scripts中,然后您无需提供驱动程序的路径,只需

1
driver= webdriver.Chrome()

将工作

可以证明这也适用于Python3.7。