我正在尝试在Jupyter笔记本中使用Selenium设置Chromedriver。我还在chromedriver所在的Windows 10系统中设置了环境变量.exe但我仍然得到以下显示的错误。
这是我的代码:
# Import Libraries
import pandas as pd
import selenium
import os
from selenium import webdriver
chrome_path = r"C:UsersKlsinghDesktopchromedriver.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 上的在线服务器上执行上述代码。 请帮忙。
谢谢 库玛
当您使用原始开关时,即r
您需要将 WebDriver 变体的绝对路径括在单引号内,即'...'
并通过密钥executable_path
调用webdriver.Chrome()
时传递它,如下所示:
因此,实际上您的代码行将是:
chrome_path = r'C:UsersKlsinghDesktopchromedriver.exe'
wd = webdriver.Chrome(executable_path=chrome_path)
或者,您可以在一行中实现相同的目标,如下所示:
wd = webdriver.Chrome(executable_path=r'C:UsersKlsinghDesktopchromedriver.exe')
我不得不解决同样的问题,只是在桌面上解决了它。
如果您使用的是适用于 Linux 或类似内容的 Windows 子系统,请确保路径键入正确且可从操作系统访问。
例如,在WSL中,/mnt/c/users/documents而不是C:/users/documents。