Selenium WebDriverException'chromedriver.exe'必须在PATH中

时间:2019-11-08 20:01:19

标签: python python-3.x selenium web-scraping

我正在尝试将硒用于python网络抓取程序,但是当我尝试运行该程序时,出现以下错误:

/usr/local/bin/python3 /Users/xxx/Documents/Python/hello.py                                     
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/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: '/Users/xxx/Documents/Python/chromedriver.exe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/xxx/Documents/Python/hello.py", line 9, in <module>
    wd = webdriver.Chrome(executable_path=DRIVER_PATH)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/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

这是python代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
from selenium import webdriver


DRIVER_PATH = '/Users/xxx/Documents/Python/chromedriver.exe'

wd = webdriver.Chrome(executable_path=DRIVER_PATH)

我认为问题是我没有在DRIVER_PATH变量中正确指定文件路径,但是我不确定

我正在使用Mac

4 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但我将 chrome 驱动器添加到了我的路径变量中,我收到一个错误,说访问被拒绝,可执行文件可能有错误的权限...请帮忙!

Traceback (most recent call last):
  File "C:\Users\austi\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\austi\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\austi\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
PermissionError: [WinError 5] Access is denied

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last):
  File "C:\Users\austi\Desktop\SCRIPTS\BestBuy-3060ti.py", line 4, in <module>
    browser = webdriver.Chrome('C:\\Users\\austi\\Documents')
  File "C:\Users\austi\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Users\austi\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 86, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'Documents' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

答案 1 :(得分:0)

我会尝试一下(只需添加'r'):

wd = webdriver.Chrome(executable_path=r'/Users/xxx/Documents/Python/chromedriver.exe')

如果您认为这是文件路径,则可以进行检查:

import os.path
os.path.exists(DRIVER_PATH)

此外,Beautifulsoup将与urllib2一起使用 https://www.pythonforbeginners.com/beautifulsoup/beautifulsoup-4-python

import urllib2
url = "https://www.URL.com"
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content)

答案 2 :(得分:0)

您需要更新static以包含您的根目录,该目录通常为DRIVER_PATH

C:\

或者,您可以按照本教程将DRIVER_PATH = 'C:/Users/xxx/Documents/Python/chromedriver.exe' 包含文件夹的路径(通常是chromedriver_win32文件夹)添加到chromedriver.exe环境变量中:

https://docs.telerik.com/teststudio/features/test-runners/add-path-environment-variables

答案 3 :(得分:0)

文件名有误。

“ chomedriver.exe”用于Windows。

如果在Mac上使用macOS和chromedriver,则文件名应为“ chomedriver”,不带“ .exe”。

我有同样的问题,但这解决了。