1.下载python2.7.exe 进行安装(selenium目前不支持python3.x)。
注意:selenium RC需要设置Python的环境变量,将其加入到 Path路径中,如“E:Python27;” ,则在原路径后加上分号E:Python27 ;且需要安装JDK 并配置起环境变量;
selenium2.0 因为不会使用到Selenium RC Server端 ,即 selenium-server-standalone-2.11.0.jar,所以不需要配置。
2.下载SetupTools.exe,是一个帮助你安装python包的第三方工具,直接安装即可
3.安装pip工具:命令提示行进入到E:Python2Scripts> ,执行 easy_install pip, 等待完成即可
4.安装 Selenium Client Drivers(In order to create scripts that interact with the Selenium Server (Selenium RC, Selenium Remote Webdriver) or create local Selenium WebDriver script you need to make use of language-specific client drivers.):
输入pip install selenium 或者 pip install –U selenium后回车,等待下载并安装,如果无错误 即安装成功。
5.下载并启动selenium RC server 端(selenium2.0无需启动):
下载http://selenium.googlecode.com/files/selenium-server-standalone-2.21.0.jar
运行:java -jar selenium-server-standalone-2.14.0.jar
6.编辑selenium RC/selenium2.0代码即可进行测试验证.
selenium RC代码 :
from selenium import selenium
import unittest, time, re
class NewTest(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "http://www.google.com/")
self.selenium.start()
def test_new(self):
sel = self.selenium sel.open("/")
sel.type("q", "selenium rc")
sel.click("btnG")
sel.wait_for_page_to_load("30000")
self.failUnless(sel.is_text_present("Results * for selenium rc"))
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
selenium2.0代码:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# go to the google home page
driver.get("http://www.google.com")
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")
# type in the search
inputElement.send_keys("Cheese!")
# submit the form (although google automatically searches now without submitting)
inputElement.submit()
# the page is ajaxy so the title is originally this:
print driver.title
try:
# we have to wait for the page to refresh, the last thing that seems to be updated is the title
WebDriverWait(driver, 10).until(lambda driver : driver.title.lower().startswith("cheese!"))
# You should see "cheese! - Google Search"
print driver.title
finally:
driver.quit()