selenium - chromedriver可执行文件需要在PATH中

Seb*_*sen 36 python selenium selenium-chromedriver

引用错误消息:'chromedriver'可执行文件需要在PATH中

我试图在pycharm中使用selenium编写脚本代码.但后来我得到了上述错误.我已将我的selenium链接到pycharm,如下所示:https://gyazo.com/b5622e3165bbfd93cfa205178df79b6f - (新鲜和最新)

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

顺便说一句,我尝试在cmd中键入"chromedriver",它不被认为是内部或外部命令.

错误如下所示:

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'
Run Code Online (Sandbox Code Playgroud)

Max*_*ers 55

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

那你有多个选择:

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

    driver = webdriver.Chrome(executable_path='C:/path/to/chromedriver.exe')
    
    Run Code Online (Sandbox Code Playgroud)

  • @mkheifetz 如果您使用的是 Mac,则 chromedriver 文件没有文件扩展名,因此它应该是“/Users/Misha/chromedriver”。但是尝试将它分配给一个变量,然后检查 `os.path.exists(chromedriver_path)` 是否返回 `True`。 (3认同)
  • 正如 @Camillo 提到的,直接指定executable_path 现在已被弃用。您现在需要通过以下方式导入 Service 类: `from selenium.webdriver.chrome.service import Service` 然后 `driver = webdriver.Chrome(service=Service(r'C:/path/to/chromedriver.exe')` (3认同)
  • 每个浏览器都有/需要自己的驱动程序,Linux 上没有 IE,Windows 上没有 Safari。Selenium 只是一个工具,它使您能够与浏览器“对话”。 (2认同)
  • 将 chromedriver 添加到路径后出现新错误。你能找出问题所在吗?它与“许可被拒绝”有关。我如何授予它权限? (2认同)

小智 23

尝试这个 :

pip install webdriver-manager
Run Code Online (Sandbox Code Playgroud)
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!我实际上正在制作一个存储库来处理所有操作系统,就像 ChromeDriverManager 一样,然后发现了这个。 (2认同)
  • 对于其他有此问题的人;从 Selinium 4.x 开始,情况略有不同。请参阅 driver-manager 的 GitHub 页面了解更多信息:https://github.com/SergeyPirogov/webdriver_manager (2认同)

the*_*guy 16

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

driver= webdriver.Chrome()
Run Code Online (Sandbox Code Playgroud)

将工作


ll2*_*2ll 9

2020 年的答案。以下代码解决了这个问题。很多刚接触硒的人似乎必须通过这一步。安装 chromedriver 并将其放在桌面上的文件夹中。还要确保将 selenium python 项目放在与 chrome 驱动程序所在的文件夹相同的文件夹中。

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

对于 Windows

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

Run Code Online (Sandbox Code Playgroud)

对于 Linux/Mac

driver = webdriver.Chrome("/home/USER_NAME/FOLDER/chromedriver")
Run Code Online (Sandbox Code Playgroud)

  • 我要补充一下,在谷歌搜索 Windows 版本时遇到了这个问题,Chocolately 有 ChromeDriver,这使得通过简单的命令更容易保持更新:https://chocolatey.org/install 然后你可以使用 `choco install chromedriver` (2认同)

Eri*_*pur 6

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

例如:

from selenium import webdriver

driver = webdriver.Chrome(executable_path='path/to/folder/chromedriver')
Run Code Online (Sandbox Code Playgroud)


mam*_*mal 5

尝试这个 :

driver = webdriver.Chrome(ChromeDriverManager().install())
Run Code Online (Sandbox Code Playgroud)