selenium RC selenium2.0 python环境搭建

本文详细介绍如何使用Selenium进行自动化测试,包括安装Python 2.7、配置环境变量、安装必要的工具如SetupTools和pip等步骤。此外还提供了两种不同版本的示例代码:seleniumRC和selenium2.0,帮助读者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:Python2\Scripts> ,执行 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值