执行以下脚本时,出现错误“'chromedriver'可执行文件必须位于PATH中”

时间:2018-12-22 13:10:58

标签: python selenium

from selenium import webdriver  
driver = webdriver.Chrome()  
driver = webdriver.Chrome(
    r'C:\Users\New User prof\Downloads\chromedriver_win32.zip\chromedriver.exe')
driver.get(
    "https://www.google.com/webmasters/tools/url-removal?hl=en&siteUrl=https://www.greatbuyz.com/&mesd=AB9YKzIL4DBt4yX8SVayjC2kUq8yeXctK_u2WGH4KlESTcjXkcOsdBvVP7TnX4S4bBF4PADFQzZAxIqcxMiVerW67kTw-UGIWjHVftlzX5DNcJjm3Uz5wBpWxkDYY7IIFlVMdiEvTsAG_GwgA_DqO7Exg5w80HGHX_lk4okr-Ay7vrCG63zKVLrMGmyNMUPGEESjX2rJF-Xx&authuser=2")

driver.find_element_by_xpath(
    "//button[@class='goog-inline-block jfk-button jfk-button-standard']").click()

错误:

Traceback (most recent call last):
  File "/root/pythonnk/my_env/lib64/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/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': 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "bot.py", line 4, in <module>
    driver = webdriver.Chrome()
  File "/root/pythonnk/my_env/lib64/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/root/pythonnk/my_env/lib64/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' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

2 个答案:

答案 0 :(得分:0)

我不是百分之百确定这是问题所在。如果路径中有空格,则有时Windows中无法识别该路径。您可以尝试在路径周围添加其他双引号。

driver = webdriver.Chrome(
    r'"C:\Users\New User prof\Downloads\chromedriver_win32.zip\chromedriver.exe"')

我无法确定天气是否需要提取。我现在不使用Windows,因此无法测试。

答案 1 :(得分:0)

  1. 请确保在使用zip文件之前先将其解压缩并提供提取的文件路径。
  2. 在给出路径时使用正斜杠。
  3. 尝试下面的代码

    from selenium import webdriver 
    driver = webdriver.Chrome(
       'C:/Users/New User prof/Downloads/chromedriver_win32.zip/chromedriver.exe')
    driver.get("URL")
    driver.find_element_by_xpath("//button[@class='goog-inline-block jfk-button jfk-  button-standard']").click()