【问题标题】:Python Chromedriver WinError 2 with Selenium带有 Selenium 的 Python Chromedriver WinError 2
【发布时间】:2020-07-24 14:52:38
【问题描述】:

我的代码中出现以下错误:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", 
line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

我尝试更改 PATH 变量,更改 chromedriver 路径无济于事。有趣的是,这个错误只是有时会发生,但我还没有找到一个模式。这是我的 chromedriver 定义:

options = Options()
options.headless = True
options.add_argument('--log-level=3')
chromepath = str(Path(os.getcwd()).parent) + r'\chromedriver.exe'
driver = webdriver.Chrome(chromepath, options=options)

【问题讨论】:

  • 如果路径中有chromedriver.exe,我认为不需要引用chromepath。

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

奇怪的是它有时只起作用。将此设置为绝对路径,它应该可以正常工作。

chromepath = str(Path(os.getcwd()).parent) + r'\chromedriver.exe'
print (chromepath) #set the output of this below
driver = webdriver.Chrome("abs path here", options=options)

更新:解决方案。结果我定义了两次驱动程序,删除了一个定义,它似乎做得很好。但不确定为什么会引发该特定错误。

【讨论】:

  • 所以不是放变量名,而是放变量的值?我会试一试,但这会如何改变事情?
  • 更新:所以如果我理解正确,那么它就不起作用了。
  • 这确实很奇怪。它有时起作用的事实很难理解。你能试试这样的driver = webdriver.Chrome(executable_path='C:/path/to/chromedriver.exe')
  • 我试一试,会不会是exe损坏了?
  • 也可以这样
猜你喜欢
  • 1970-01-01
  • 2019-12-27
  • 2017-02-09
  • 2017-01-16
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
相关资源
最近更新 更多