Selenium firefox仅在通过Windows Task Scheduler运行时无法加载 [英] Selenium firefox fails to load only when run through windows task scheduler

查看:107
本文介绍了Selenium firefox仅在通过Windows Task Scheduler运行时无法加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a web scraping script in Python, using Selenium and (tor) Firefox. This runs completely fine when I start the script manually, in my IDE or from cmd. However, this script needs to be run when I am not in the office, so I run it via a batch script (along with some other scraping scripts) which is started by the windows task scheduler. Again, when I run this batch file manually, the script runs perfectly. When the scheduled task runs, on the other hand, it fails as soon as the webdriver is set with the following unhelpful error message:

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

有人知道为什么只有从任务计划程序运行时才会发生此错误吗?

Does anyone have any idea why this error occurs only when run from the task scheduler?

推荐答案

进一步调查后,我发现在后台处理任务时会发生错误.当任务设置为无论用户是否登录都运行"时,会发生这种情况.将此设置更改为仅在用户登录后才运行",可使任务在前台运行而不会出现问题.

After further investigation, I found that the error happens when the task is processed in the background. This happens when the task is set to 'Run whether user is logged on or not'. Changing this setting to 'Run only when user is logged on' allows the task to run without issue, in the foreground.

由于某种原因,在后台运行会导致Firefox崩溃,但是chromedriver(我在运行的其他一些抓取脚本中使用过)不受此影响.这似乎有些怪异,但是这种解决方法足以使它在Windows上运行.

For some reason, running in the background causes firefox to crash, but chromedriver (which is used in some other scraping scripts I run) is unaffected by this. This seems slightly bizarre, but this workaround is sufficient to get this running on windows.

这篇关于Selenium firefox仅在通过Windows Task Scheduler运行时无法加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆