【问题标题】:Selenium firefox fails to load only when run through windows task schedulerSelenium firefox 仅在通过 Windows 任务调度程序运行时无法加载
【发布时间】:2018-04-26 01:24:31
【问题描述】:

我有一个使用 Selenium 和 (tor) Firefox 的 Python 网页抓取脚本。当我在我的 IDE 或 cmd 中手动启动脚本时,它运行得非常好。但是,当我不在办公室时,需要运行此脚本,因此我通过 Windows 任务调度程序启动的批处理脚本(以及其他一些抓取脚本)运行它。同样,当我手动运行此批处理文件时,脚本运行完美。另一方面,当计划任务运行时,一旦设置了 webdriver 并显示以下无用的错误消息,它就会失败:

Traceback (most recent call last):
  ...
  #private library traceback
  ...
  File "redacted.py", line 322, in redacted_func
    driver = webdriver.Firefox()
  File "C:\Program Files\Python 3.5\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
    keep_alive=True)
  File "C:\Program Files\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Program Files\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Program Files\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files\Python 3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 0

有谁知道为什么这个错误只有在从任务调度程序运行时才会发生?

【问题讨论】:

  • 您是否以其他用户身份运行计划任务?
  • 任务正在以可以手动成功运行进程的同一用户身份运行。

标签: python windows selenium firefox


【解决方案1】:

经过进一步排查,发现该错误是在后台处理任务时发生的。当任务设置为“无论用户是否登录都运行”时会发生这种情况。将此设置更改为“仅在用户登录时运行”允许任务在前台运行而不会出现问题。

由于某种原因,在后台运行会导致 firefox 崩溃,但 chromedriver(在我运行的其他一些抓取脚本中使用)不受此影响。这看起来有点奇怪,但这种解决方法足以让它在 Windows 上运行。

【讨论】:

  • 设置“仅在用户登录时运行”确实有效,但是您错过了自动运行计划任务的要点,因为您需要用户在登录时实际登录到服务器计划任务应该运行
  • 我看不出这是怎么回事?您要么需要一个错误修复,以便 firefox 在后台运行而不会出现错误(我无法帮助您),要么登录用户以交互方式运行任务。您仍然可以随时运行任务,这毕竟是任务调度程序的重点......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 1970-01-01
相关资源
最近更新 更多