简体   繁体   English

使用 ChromeDriver 运行计划任务

[英]Run a scheduled task with ChromeDriver

I have made a script based on Selenium and Chromedriver.我制作了一个基于 Selenium 和 Chromedriver 的脚本。 Basically a program that login into a site.基本上是一个登录网站的程序。 Writes a comment (From a txt file from computer) and then closes the program and no it is not a spam script but a script I have made just to begin with python and selenium.写一条评论(来自计算机的 txt 文件)然后关闭程序,不,它不是垃圾邮件脚本,而是我刚刚从 python 和 selenium 开始制作的脚本。

The program itself works very well if I start it manually.如果我手动启动程序,程序本身运行良好。 Then there is no issue and chromedriver is headless since I don't need to see the whole process chrome_options.add_argument("--headless")然后就没有问题了,chromedriver 是无头的,因为我不需要查看整个过程chrome_options.add_argument("--headless")

Then I saw a post from here Scheduling a Python Script然后我从这里看到了一个帖子调度 Python 脚本

and I did follow it我确实遵循了它设置

but the issue im having is that everytime it is the time and the program starts.但我遇到的问题是,每次都是时间,程序开始了。 It comes up the script and then a fast error which I managed to print它出现了脚本,然后是我设法打印的快速错误

错误

Which I can see there is a issue with the Chromedriver.我可以看到 Chromedriver 存在问题。 The thing is now.事情就是现在。 How can I make this script to work through schedule tasks with Chromedriver running on the background.如何使此脚本在 Chromedriver 在后台运行的情况下完成计划任务。 I might have done the setup wrong but the program works manually so I guess there might be a issue with Windows schedule tasks?我可能做错了设置,但程序手动运行,所以我猜 Windows 计划任务可能有问题?

Basically I just want the script to run on the background every xx:xx time.基本上我只希望脚本每隔 xx:xx 时间在后台运行。

Please feel free to comment if needed something more information.如果需要更多信息,请随时发表评论。

    chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument("--headless")
chrome_options.add_argument("--user-agent=Mozilla/5.0 (Linux; Android 6.0; HTC One M9 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.implicitly_wait(5)

The driver you are using is too old and does not recognize ChromeHeadless, you need to use version 2.29 or newer:您使用的驱动程序太旧,无法识别 ChromeHeadless,您需要使用 2.29 或更高版本:

Selenium ChromeDriver does not recognize newly compiled Headless Chromium (Python) Selenium ChromeDriver 无法识别新编译的 Headless Chromium (Python)

我遇到了同样的问题(使用较新的“无头”版本),解决方案是将 Windows 计划任务作为“管理员”(使用“s”)运行。

I had this problem and almost give up from task scheduler and started to write windows service which would run my web scraping application.我遇到了这个问题,几乎放弃了任务调度程序并开始编写将运行我的网络抓取应用程序的 Windows 服务。 If you got exception chrome unreachable or chrome didn't get response from HTTP server...如果您遇到异常 chrome unreachable 或 chrome 没有从 HTTP 服务器获得响应...

This make my application work even through task scheduler.这使我的应用程序甚至可以通过任务调度程序工作。

Task Scheduler -> Go on task -> Properties -> Conditions > Under Network -> Check Network Start only if the following network connection is available -> Select "Any connection".任务计划程序 -> 继续任务 -> 属性 -> 条件 > 在网络下 -> 仅当以下网络连接可用时才检查网络启动 -> 选择“任何连接”。

Go this from this post windows-10-task-scheduler-not-running从这篇文章windows-10-task-scheduler-not-running 开始

When using selenium with Chrome, you have to make sure that your selenium version is compatible with the chrome version you have installed.在 Chrome 中使用 selenium 时,您必须确保您的 selenium 版本与您安装的 chrome 版本兼容。 If you get the app to work and then update your chrome you might get an unrecognized version error.如果您让应用程序运行,然后更新您的 chrome,您可能会收到无法识别的版本错误。

If you have chrome v 102.0 like me, you need to get the latest selenium chrome driver that is the same version as your browser.如果您像我一样拥有 chrome v 102.0,则需要获取与您的浏览器版本相同的最新 selenium chrome 驱动程序。

See Example Below:请参见下面的示例:

铬版

Chrome Driver Download: https://chromedriver.chromium.org/downloads Chrome 驱动下载: https ://chromedriver.chromium.org/downloads Chrome 驱动下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM