【发布时间】:2020-03-08 22:54:36
【问题描述】:
安装了 Selenium 并构建了最新的稳定 Chromedriver。
按照建议我把它放到环境变量路径中:
我的代码如下所示:
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('./driver/chromedriver', options=options)
timeout = 3
我得到的错误是:
WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
我什至在命令行中尝试了以下操作:
chromedriver --whitelisted-ips=""
谁能帮我解决这个问题。
整个输出错误是这样的:
FileNotFoundError Traceback(最近一次调用最后一次) ~\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py in start(self) 75 标准错误=self.log_file, ---> 76 标准输入=管道) 77 除了 TypeError:
~\Anaconda3\lib\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、编码、错误、文本) 第774章 --> 775 恢复信号,开始新会话) 776 除了:
~\Anaconda3\lib\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, used_restore_signals ,未使用的_start_new_session) 第1177章 -> 1178 启动信息) 第1179章:
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
WebDriverException Traceback (most recent call last)
<ipython-input-9-3b7827e2d128> in <module>
2 from selenium import webdriver
3
----> 4 driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
5 driver.get('http://www.google.com/');
6 time.sleep(5) # Let the user actually see something!
~\Anaconda3\lib\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:
~\Anaconda3\lib\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:
【问题讨论】:
-
只需将 chromedriver 放入 c:\windows - 将垃圾添加到您的路径是没有意义的。
-
我希望这是个玩笑。将垃圾添加到 Windows 目录比将垃圾添加到路径要糟糕得多。 (在调用脚本之前,可以选择在本地更改 PATH。)
标签: python selenium web-scraping selenium-chromedriver