您的位置:首页 > 编程语言 > Python开发

python学习(一)Use Python to Drive Selenium RC

2012-02-24 14:31 281 查看
Selenium RC支持多种编程语言驱动客户端浏览器,这里主要介绍使用Python在Windows下驱动Selenium RC。Python是一种面向对象的解释性的计算机程序设计语言。

1. 准备工作

安装jdk。jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe

安装python。python-2.7.2.msi

下载selenium RC。selenium-remote-control-1.0.3

2.启动selenium server。解压selenium RC ,在selenium-server.jar目录下通过运行命令java -jar selenium-server.jar -interactive启动服务。

可以通过写批处理脚本,启动。如下:

@echo off
title Selenium Server
D:
cd D:\selenium\selenium-remote-control-1.0.3\selenium-server-1.0.3
java -jar selenium-server.jar -interactive

3.在IE上搜索hello world为例:

View Code

from selenium import selenium
import unittest

class Untitled(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*iexplore", "http://www.google.com/webhp")
self.selenium.start()

def test_untitled(self):
sel = self.selenium
sel.open("http://www.google.com.hk/")
sel.type("q", "hello world")
sel.click("name=btnG")
sel.wait_for_page_to_load(8000)
self.assertEqual("hello world - Google 搜索",sel.get_title())

#def tearDown(self):
#self.selenium.stop()
#self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
unittest.main()


这里,仅使用*iexplore则表示浏览器安装在默认的路径,即IE安装在"C:\Program Files\Internet Explorer\iexplore.exe",如果不是安装在默认的路径,需要指明浏览器安装的地址,如:"*firefox D:\Program Files\Mozilla Firefox\\firefox.exe"。

"def tearDown(self):
self.selenium.stop()"

这段表示浏览器运行结束后直接关闭浏览器,这里可以注释掉。

在python上点击F5开始运行python代码。通过selenium直接调用IE浏览器进行客户端运行。

同时命令行窗口显示Selenium Server进行的每个步骤操作。

note:使用firefox浏览器时需注意,目前Selenium RC不支持firefox3.0版本,不管是使用"*chrome"还是"*firefox",都无法把Firefox调出;但是如果机器上使用的Firefox是低于3.0版本,那么可以直接使用"*chrome"这个参数。

在运行之前,需要将Firefox中的代理设置成和Selenium Server一致,Localhost,端口为4444。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: