python webdriver自动化测试初步印象
以下示例演示启动firefox,浏览google.com,搜索Cheese,等待搜索结果,然后打印出搜索结果页的标题
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
# 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(EC.title_contains("cheese!"))
# You should see "cheese! - Google Search"
print driver.title
finally:
driver.quit()
python webdriver自动化测试通过控件xpath定位元素
有一段html代码如下:
现在通过xpath来查找到相应的input元素,代码demo如下:
from selenium.webdriver.common.by import By
#查找所有input元素方法一
inputs = driver.find_elements_by_xpath("//input")
#查找所有input元素方法二
inputs = driver.find_elements(By.XPATH, "//input")
#查找指定的input元素,比如查找name为other的input
inputs = driver.find_element_by_xpath("//input[@name='other']")
python webdriver自动化测试在window和frame之间切换
from selenium import webdriver
# 启动firefox初始化webdriver
driver = webdriver.Firefox()
1、切换到指定窗口名的窗口,
例如有一段html代码如下
driver.switch_to_window("windowName")
2、当然也可以通过句柄来切换,示例如下
for handle in driver.window_handles:
driver.switch_to_window(handle)
上述代码会遍历,一个个的切换。
3、通过frame名称切换到指定的frame
driver.switch_to_frame("frameName")
4、也可以通过frame的索引来切换
driver.switch_to_frame(0) #切换到第一个frame
python使用selenium rc和webdriver启动不同浏览器的方法
Selenium 1 -启动 Internet Explorer
from selenium import selenium
selenium = selenium("localhost", 4444, "*iexplore", "http://google.com/")
selenium.start()
Selenium 1 - 启动Firefox
from selenium import selenium
selenium = selenium("localhost", 4444, "*firefox", "http://google.com/")
selenium.start()
webdriver - 启动Firefox
from selenium import webdriver
driver = webdriver.Firefox()
webdriver - 启动Chrome
from selenium import webdriver
driver = webdriver.Chrome()
webdriver - 启动Remote
from selenium import webdriver
driver = webdriver.Remote( browser_name="firefox", platform="any")
webdriver - 启动IE
from selenium import webdriver
driver = webdriver.Ie()
备注: 除了启动IE外,webdriver启动其他浏览器均需安装相应浏览器的驱动组件,关于这块的环境搭建请参见
python webdriver自动化测试通过控件css定位元素
有一段html代码如下:
milk
cheese
现在通过css来查找到相应的span元素,代码demo如下:
from selenium.webdriver.common.by import By
#查找css为span元素方法一
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")
#查找css为span元素方法二
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")
python webdriver自动化测试通过控件 Partial Link Text定位元素
有一段html代码如下:
现在通过Partial Link Text来查找到相应的a元素,代码demo如下:
from selenium.webdriver.common.by import By
#查找Partial Link Text为a元素方法一
cheese = driver.find_element_by_partial_link_text("cheese")
#查找Partial Link Text为a元素方法二
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")
python webdriver自动化测试通过控件link text定位元素
有一段html代码如下:
现在通过link text来查找到相应的a元素,代码demo如下:
from selenium.webdriver.common.by import By
#查找link text为cheese元素方法一
cheese = driver.find_element_by_link_text("cheese")
#查找link text为cheese元素方法二
cheese = driver.find_element(By.LINK_TEXT, "cheese")
python webdriver自动化测试通过控件tag name定位元素
有一段html代码如下:
现在通过tag name来查找到相应的iframe元素,代码demo如下:
from selenium.webdriver.common.by import By
#查找tag name为iframe元素方法一
frame = driver.find_element_by_tag_name("iframe")
#查找tag name为iframe元素方法二
frame = driver.find_element(By.TAG_NAME, "iframe")
python webdriver自动化测试通过控件class name定位元素
有一段html代码如下:
现在通过class name来查找到相应的div元素,代码demo如下:
from selenium.webdriver.common.by import By
#查找第一个class为cheese的div元素
cheeses = driver.find_elements_by_class_name("cheese")
# 查找所有class为cheese的div元素
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")
python webdriver自动化测试通过控件name定位元素
#导入webdriver
from selenium import webdriver
# 启动firefox初始化webdriver
# ie: driver = webdriver.Ie()
# chrome: driver = webdriver.Chrome()
driver = webdriver.Firefox()
# 访问baidu官网
driver.get("http://www.baidu.com")
# 定位输入框
element= driver.find_element_by_name("wd")
# 打印出来看一下
# 会看到一个内存地址,说明已经找到
print element
# 关闭浏览器、退出webdriver
driver.quit()
python webdriver自动化测试通过控件id定位元素
#导入webdriver
from selenium import webdriver
# 启动firefox初始化webdriver
# ie: driver = webdriver.Ie()
# chrome: driver = webdriver.Chrome()
driver = webdriver.Firefox()
# 访问baidu官网
driver.get("http://www.baidu.com")
# 定位输入框
element = driver.find_element_by_id("kw")
# 打印出来看一下
# 会看到一个内存地址,说明已经找到
print element
# 关闭浏览器、退出webdriver
driver.quit()
python webdriver自动化测试访问某个网址
python webdriver自动化测试访问指定网址示例
#导入webdriver
from selenium import webdriver
# 启动firefox初始化webdriver
# ie: driver = webdriver.Ie()
# chrome: driver = webdriver.Chrome()
driver = webdriver.Firefox()
# 访问google官网
# 记得网址前最好带http
driver.get("http://www.google.com")
- Python WebDriver自动化测试
转载来自: http://www.cnblogs.com/fnng/p/3160606.html Webdriver Selenium 是 ThroughtWorks 一个强大的基于浏览器的开源自动化 ...
- python webdriver 自动化测试练习 1-- 在线调查
__author__ = 'Mickey0s' # coding:utf8 from selenium import webdriver from selenium.webdriver.common. ...
- 前端自动化测试python+webdriver
前言:很多做测试的朋友的就知道,python+webdriver 可以做自动化测试,这对前端开发是非常有用的. python 入门我就不讲了 ,推荐学习 廖雪峰老师的python入门3.5新版哈 ...
- 转:python webdriver API 之简单对象的定位
对象(元素)的定位和操作是自动化测试的核心部分,其中操作又是建立在定位的基础上的,因此元素定位就显得非常重要. (本书中用到的对象与元素同为一个事物)一个对象就像是一个人,他会有各种的特征(属性) , ...
- 转:python webdriver 环境搭建
第一节 环境搭建准备工具如下:-------------------------------------------------------------下载 python[python 开发环境]ht ...
- python selenium 自动化测试web
如何使用python完成自动化测试web页面呢?首选selenium 那基于python的selenium如何使用,下面看一段测试案例: 基于python的selenium 安装方法: pip i ...
- 基于Appium、Python的自动化测试
基于Appium.Python的自动化测试环境部署和实践 第一章 导言 1.1 编制目的 该文档为选用Appium作为移动设备原生(Native).混合(Hybrid).移动Web(Mobile ...
- [转]构建Python+Selenium2自动化测试环境(二)
构建Python+Selenium2自动化测试环境完成之后,就需要测试支持python的selenium的版本是否都支持在不同浏览器上运行,当前我们分别在三个最通用的浏览器上通过脚本来测试. 1.在I ...
- Python Web自动化测试入门与实战,从入门到入行
Python Web自动化测试入门与实战 购买地址 · 京东:https://item.jd.com/69239480564.html 天猫:https://detail.tmall.com/it ...
随机推荐
- ZOJ1081 Points Within 点和多边形的位置关系
ZOJ1081 给一个点和一个多边形 判断点在多边形内(边上)还是在多边形外 在多边形外的点引一条射线必然穿过多边形的两条边 而在多边形内的点则不一定. 当然凹多边形有特殊情况 但是总能找到对应位置关 ...
- oracle 统计/分析函数
Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行. 语法: Sql代码 <analytic ...
- linux下导入oracle数据表
提前说明:这个是默认oracle已经安装好切数据库默认表空间已经创建好.之后将数据表dmp文件直接导入到默认表空间里(默认表空间不用再指定,因为创建数据库时已经指定默认表空间) linux命令如下: ...
- 解方程 2014NOIP提高组 (数学)
解方程 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入描述 Input Description 输入文 ...
- golang——随机数(math/rand包与crypto/rand包)
1.math/rand 包 1.1.math/rand 包实现了伪随机数生成器 1.2.主要方法 (1)func Seed(seed int64) 设置随机种子,不设置则默认Seed(1) (2)fu ...
- c#自定义ORM框架---(泛型&反射&实体类扩展属性<附带通用增、删、查、改>)
该教材主要是运用到泛型.反射和实体类扩展属性 步骤一.建立扩展属性类 实体类扩展属性要继承Attribute基类完成 [AttributeUsage(AttributeTargets.Property ...
- redis实际项目作用
我整理一下redis主要在项目作用,只是我接触到的 1 手机验证码存入redis中,可以限制什么时候有效 2 防止接口请求频率过高,例如一分钟只能请求5次 代码如下: <?php /** * ...
- 浅谈算法——splay
BST(二叉查找树)是个有意思的东西,种类巨TM多,然后我们今天不讲其他的,我们今天就讲splay 首先,如果你不知道Splay是啥,你也得知道BST是啥 如上图就是一棵优美的BST,它对于每个点保证 ...
- Snipaste强大离线/在线截屏软件的下载、安装和使用
步骤一: https://zh.snipaste.com/ ,去此官网下载. 步骤二:由于此是个绿色软件,直接解压即可. 步骤三:使用,见官网.ttps://zh.snipaste.com 按F1 ...
- [转]深入ASP.NET MVC之二:路由模块如何工作
本文转自:http://www.cnblogs.com/yinzixin/archive/2012/11/05/2754483.html 摘要: 上文分析了UrlRouting模块何时会被触发,本文重 ...