Python 3.x 使用selenium设置Chromedriver时出现文件未找到错误
我试图在Jupyter笔记本中使用selenium设置Chromedriver。我也在chromedriver.exe所在的Windows 10系统中设置了环境变量,但我仍然看到下面显示的错误 这是我的密码:Python 3.x 使用selenium设置Chromedriver时出现文件未找到错误,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,我试图在Jupyter笔记本中使用selenium设置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\chromedr
# 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
---------------------------------------------------------------------------
FileNotFoundError回溯(最近一次调用上次)
/开始时使用opt/conda/envs/Python36/lib/python3.6/site-packages/selenium/webdriver/common/service.py(self)
75 stderr=self.log_文件,
--->76标准偏差=管道)
77除类型错误外:
/opt/conda/envs/Python36/lib/python3.6/subprocess.py in_uuuinit_uuu(self、args、bufsize、executable、stdin、stdout、stderr、preexec_fn、close_fds、shell、cwd、env、universal_换行、startupinfo、creationflags、restore_信号、start_new_会话、pass_fds、编码、错误)
728错误读取,错误写入,
-->729恢复信号,启动新会话)
730除:
/opt/conda/envs/Python36/lib/python3.6/subprocess.py in_execute_child(self、args、execute、preexec_fn、close_fds、pass_fds、cwd、env、startupinfo、creationflags、shell、p2cread、p2c2cwrite、c2pwrite、errread、errwrite、restore_信号、start_new_会话)
1363 err_msg+=':'+repr(err_文件名)
->1364引发子项异常类型(errno\u num、err\u msg、err\u filename)
1365引发子项异常类型(错误消息)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“chromedriver”:“chromedriver”
在处理上述异常期间,发生了另一个异常:
WebDriverException回溯(最近一次调用上次)
在里面
---->1 wd=webdriver.Chrome()
2 wd.get(url)
/opt/conda/envs/Python36/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py in_uuuuuinit_uuuuuuu(self、可执行路径、端口、选项、服务参数、所需功能、服务日志路径、chrome选项、保持活动状态)
71服务参数=服务参数,
72日志路径=服务日志路径)
--->73自助服务启动()
74
75尝试:
/开始时使用opt/conda/envs/Python36/lib/python3.6/site-packages/selenium/webdriver/common/service.py(self)
81提出WebDriverException(
82“'%s'可执行文件需要位于路径中。%s”%(
--->83 os.path.basename(self.path)、self.start\u错误消息)
84 )
85 elif err.errno==errno.EACCES:
WebDriverException:消息:“chromedriver”可执行文件需要位于路径中。请看https://sites.google.com/a/chromium.org/chromedriver/home
我试图在联机服务器上执行上述代码,而不是在桌面或IDE上。
请帮忙
谢谢,
Kumar当您使用原始开关,即r
时,您需要将变量的绝对路径括在单引号内,即'…
中,并在调用webdriver.Chrome()
时通过键可执行路径传递它,如下所示:
wd = webdriver.Chrome(executable_path=r'C:\Users\Klsingh\Desktop\chromedriver.exe')
因此,您的代码行实际上是:
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')
我不得不解决同样的问题,只是在桌面上解决了它
如果您正在使用Windows subsystem for linux或类似的工具,请确保键入的路径正确并且可以从操作系统访问
例如,在我的WSL中,/mnt/c/users/documents而不是c:/users/documents我仍然收到文件未找到错误,这与我正在使用的远程解释器有关吗?我正在使用Microsoft Azure在浏览器中运行Python Jupyter笔记本,请建议如何解决此错误-WebDriverException:消息:“C:\Users\Klsingh\Desktop\chromedriver.exe”可执行文件需要位于路径中。我已经在本地系统的环境变量中添加了路径。@Klsingh如果这个/任何答案对您有帮助,对将来的读者有帮助,请告诉我答案。当任何人在笔记本电脑或台式机上使用IDE时,这个答案对本地环境有帮助。此答案不适用于Azure或IBM Watson环境。