代码无法将chromedriver.exe识别为文件

时间:2019-04-02 17:22:18

标签: python selenium-webdriver selenium-chromedriver

所以我是python的新手,并对WebDriver及其可能的应用程序感兴趣。因此,我编写此代码只是为了测试是否已安装所有必需的库。

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path= r'chromedriver.exe')
driver.get('http://www.google.com')

一旦我编写了python3 nameOfCode.py,结果就是一个错误,就像通常这样。错误是

macs-MacBook-Pro:Desktop mac$ python3 testSelenium.py
Traceback (most recent call last):
  File "testSelenium.py", line 5, in <module>
    driver = webdriver.Chrome(executable_path= mac/Desktop/chromedriver.exe)
NameError: name 'mac' is not defined
macs-MacBook-Pro:Desktop mac$ python3 testSelenium.py
Traceback (most recent call last):
  File "testSelenium.py", line 5, in <module>
    driver = webdriver.Chrome(executable_path= Desktop/chromedriver.exe)
NameError: name 'Desktop' is not defined
macs-MacBook-Pro:Desktop mac$ python3 testSelenium.py
Traceback (most recent call last):
  File "testSelenium.py", line 5, in <module>
    driver = webdriver.Chrome(executable_path= chromedriver.exe)
NameError: name 'chromedriver' is not defined
macs-MacBook-Pro:Desktop mac$ python3 testSelenium.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver.exe': 'chromedriver.exe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "testSelenium.py", line 5, in <module>
    driver = webdriver.Chrome(executable_path= r'chromedriver.exe')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

对不起,如果这很多的话:)) 达里奥

1 个答案:

答案 0 :(得分:1)

最后一行说明了一切...

'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

如果您使用的是Windows:

您需要将“ chromedriver.exe”的位置添加到名为PATH的环境变量中。您应该能够通过开始菜单搜索(搜索:环境变量)找到对话框,然后找到名为PATH的变量(不区分大小写)。如果不存在,请创建它。此变量的内容是由分号分隔的文件夹位置的列表,并且包含“ chromedriver.exe”的文件夹必须存在。

如果您使用的是Mac :(我没有,所以我无法确认这些方法)

编辑“ / etc / paths”文件以找到可执行文件的位置,或放置:

PATH="${PATH}:/some/other/directory:/another/place/scripts/live:"

export $PATH

.bash_profile 文件中的某个位置