使用硒设置Chromedriver时出现文件未找到错误

时间:2020-01-27 07:20:03

标签: python-3.x selenium selenium-webdriver selenium-chromedriver

我正在尝试使用Jupyter笔记本电脑中的硒设置Chromedriver。我还在chromedriver.exe所在的Windows 10系统中设置了环境变量,但仍然出现以下显示的错误。

这是我的代码:

# Import Libraries
import pandas as pd
import selenium
import os
from selenium import webdriver
chrome_path = r"C:\Users\Klsingh\Desktop\chromedriver.exe"

wd = webdriver.Chrome()
wd.get(url)

以下是我得到的错误,尝试了几种方法,但仍无法解决。

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/opt/conda/envs/Python36/lib/python3.6/site-packages/selenium/webdriver/common/service.py in start(self)
     75                                             stderr=self.log_file,
---> 76                                             stdin=PIPE)
     77         except TypeError:

/opt/conda/envs/Python36/lib/python3.6/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    728                                 errread, errwrite,
--> 729                                 restore_signals, start_new_session)
    730         except:

/opt/conda/envs/Python36/lib/python3.6/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1363                             err_msg += ': ' + repr(err_filename)
-> 1364                     raise child_exception_type(errno_num, err_msg, err_filename)
   1365                 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
<ipython-input-44-03162065f481> in <module>
----> 1 wd = webdriver.Chrome()
      2 wd.get(url)

/opt/conda/envs/Python36/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     71             service_args=service_args,
     72             log_path=service_log_path)
---> 73         self.service.start()
     74 
     75         try:

/opt/conda/envs/Python36/lib/python3.6/site-packages/selenium/webdriver/common/service.py in start(self)
     81                 raise WebDriverException(
     82                     "'%s' executable needs to be in PATH. %s" % (
---> 83                         os.path.basename(self.path), self.start_error_message)
     84                 )
     85             elif err.errno == errno.EACCES:

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

我正在尝试在不在台式机或IDE上的在线服务器上执行上述代码。 请帮忙。

谢谢, 库马尔

2 个答案:

答案 0 :(得分:1)

使用 raw 开关(即 r )时,您需要封装{{3}的绝对路径 } 单引号中的变体,即'...',并在通过 Key webdriver.Chrome() 调用executable_path时传递如下:

因此,有效的代码行将是:

chrome_path = r'C:\Users\Klsingh\Desktop\chromedriver.exe'

wd = webdriver.Chrome(executable_path=chrome_path)

或者,您也可以在一行中实现以下目标:

wd = webdriver.Chrome(executable_path=r'C:\Users\Klsingh\Desktop\chromedriver.exe')

答案 1 :(得分:0)

我不得不遇到同样的问题,只是在台式机上解决了这个问题。

如果您将Windows子系统用于Linux或类似的操作系统,请确保路径键入正确且可从OS访问。

例如,在WSL中,以/ mnt / c / users / documents代替C:/ users / documents