首页 > 解决方案 > 'chromedriver' 可执行文件需要在 PATH 中,但它已经存在

问题描述

我想用 Python向这个网站发送消息。

也就是说,使用 python 执行以下操作:

水图像介绍

这就是我使用 Selenium 尝试以下脚本的原因:

api_location = 'http://iphoneapp.spareroom.co.uk'
api_search_endpoint = 'flatshares'
api_details_endpoint = 'flatshares'

location = 'http://www.spareroom.co.uk'
details_endpoint = 'flatshare/flatshare_detail.pl?flatshare_id='
def contact_room(self, room_id):
    url = '{location}/{endpoint}/{id}?format=json'.format(location=self.api_location, endpoint=self.api_details_endpoint, id=room_id)
    from selenium import webdriver

    driver = webdriver.Chrome()
    # Go to your page url
    driver.get(url)
    # Get button you are going to click by its id ( also you could use find_element_by_css_selector to get element by css selector)
    button_element = driver.find_element_by_id('button id')
    button_element.click()

但它返回:

C:\Users\antoi\Documents\Programming\projects\roomfinder>python test_message.py
Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Python36\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test_message.py", line 21, in <module>
    contact_room(13829371)
  File "test_message.py", line 14, in contact_room
    driver = webdriver.Chrome()  # Optional argument, if not specified will search path.
  File "C:\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Python36\lib\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' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

虽然我已经在 PATH 中添加了它:

水图像介绍

我是 javascript 学习者。如果您有技巧和时间来展示如何在 Javascript 中回答问题,我总是很乐意学习 :)

标签: javascriptpythonpython-3.x

解决方案


chromedriver 需要位于 python 脚本的路径中,或者您需要将其添加到驱动程序中:

driver_path = 'Path\to\your\Driver'
driver = webdriver.Chrome(executable_path = driver_path)

webdriver.Firefox()如果你谈论,你为什么要使用Chrome


推荐阅读