Selenium + python 测试环境搭建扩展-HTMLUNIT的使用

尝试给公司的网站写每日例行检查的脚本时,不需要去打开浏览器,这是就用到HTMLUNIT的使用

HTMLUNIT是基于Selenium服务端的,所以需要selenium-server-standalone-XXX.jar

1、下载安装java ,为了运行jar包

2、下载selenium-server-standalone-XXX.jar ,这里特别要注意版本,要跟自己的selenium版本匹配

     查看自己已经安装的包的方法,cmd命令进入安装Python的目录下的Scripts

     输入 pip list即可查看所有已经安装的包的版本号

3、以上两步完成后,cmd进入到selenium-server-standalone-XXX.jar 的安装目录,

    cmd命令输入 java -jar selenium-server-standalone-xxx.jar,代理服务器就跑起来啦

    PS:如果无法启动,可能是端口被占用了,可以下载tcpview查看所有端口

4、最后一步就是测试一下HTMLUNIT能否正常使用了

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver

htmlunit = webdriver.Remote(desired_capabilities=DesiredCapabilities.HTMLUNIT)
htmlunit.get("http://www.baidu.com")
print htmlunit.title

输出结果:
C:\Python27\python.exe F:/soft_Test/python/code/HTMLUnit_Test.py 百度一下,你就知道 Process finished with exit code 0

  

 

posted @ 2015-10-24 16:53  一朵菊花向阳开  阅读(2563)  评论(0编辑  收藏  举报