您现在的位置:首页 >> 前端 >> 内容

使用Seleniumserverstandalone实现定时web自动化下载电影

时间:2017/3/24 9:11:50 点击:

  核心提示:1. 录制编写Selenium脚本进行Selenium IDE环境的部署和脚本录制。并将脚本保存为test suite(测试套件),例如命名为:Baidu.html。例如,将它保存在C:\test目录...

1. 录制编写Selenium脚本

进行Selenium IDE环境的部署和脚本录制。并将脚本保存为test suite(测试套件),例如命名为:Baidu.html。例如,将它保存在C:\test目录下。

使用Seleniumserverstandalone实现定时web自动化下载电影

2. 下载Selenium-server-standalone JAR包

为什么要下载Selenium-server-standalone JAR包?

官网说明:

The Selenium Server is needed in order to run Remote Selenium

WebDriver. Selenium 3.X is no longer capable of running Selenium RC

directly, rather it does it through emulation and the

WebDriverBackedSelenium interface.

翻译一下就是:如果想要运行远程Selenium服务(脱离Firefox上的SeleniumIDE)来运行的话,就需要这个JAR包,Selenium3.x开始不再支持直接运行Selenium RC。

(所以我们要找一个2.x版本的Selenium-server-standalone.jar包)

可以在Selenium官网下载:https://www.seleniumhq.org/download/

但国内访问很不稳定,不一定能访问成功,请尝试搜索其他下载资源。

本文将以selenium-server-standalone-2.53.1.jar为例,将此jar包下载后放在C:\test目录下。可以输入help命令,查看一些支持的参数命令:

java -jar C:test\selenium-server-standalone-2.53.1.jar -help

使用Seleniumserverstandalone实现定时web自动化下载电影

查看到一些常用的Options如下:

- -browserTimeout: 顾名思义,浏览器的超时时间设定

- -firefoxProfileTemplate,只支持Firefox,启用指定的Firefox Profile。不指定的话,会使用一个空的全新的。(你可以创建一个,专用于此测试)

- -port,Selenium server所有的端口,默认4444

- -htmlSuite,指定要运行的脚本套件

-htmlSuite requires you to specify: browserString (e.g. “*firefox”) startURL (e.g. “https://www.google.com“) suiteFile (e.g. “c:\absolute\path\to\my\HTMLSuite.html”) resultFile (e.g. “c:\absolute\path\to\my\results.html”)

知道了这些选项命令后就可以将我们前面录制的脚本拿来进行测试。

java -jar C:\test\selenium-server-standalone-2.53.1.jar -htmlSuite "*firefox" "https://www.baidu.com" "C:\test\Baidu.html" "C:\test\report.html" -log selenium-alone.log -browserTimeout 99999

为保证顺利执行,请以管理员身份运行cmd命令行。

至此,我们可以用selenium-server-standalone-2.53.1.jar来运行打开浏览器,并自动化执行脚本了,那么如何做到让系统定时执行呢?

设置Windows的计划任务

在开始程序中输入,【计划任务程序】

使用Seleniumserverstandalone实现定时web自动化下载电影

输入一个名称,如Baidu。点击【触发器】,设定一个时间,如每天晚上8点。

使用Seleniumserverstandalone实现定时web自动化下载电影

最重要的是在【操作】中设定:

点击【新建】-> 操作(启动程序)-> 浏览(选择java bin目录下的java.exe),最后将上面的代码输入到【添加参数】

java -jar C:\test\selenium-server-standalone-2.53.1.jar -htmlSuite "*firefox" "https://www.baidu.com" "C:\test\Baidu.html" "C:\test\report.html" -log selenium-alone.log -browserTimeout 99999
使用Seleniumserverstandalone实现定时web自动化下载电影

最后保存后,只要机器打开着,就会每天指定的晚上8点开始运行这个脚本。

将脚本修改成点击某个电影网站下载的步骤,即可完成。

Tags:使用 用S SE EL 
作者:网络 来源:不详