5,实战操作
使用google翻译
操作步骤
#获取输入框并输入文字
>>> content = driver.find_element_by_class_name('tlid-source-text-input')
>>> content.send_keys("hello girl")
#清空输入框内容
>>> content.clear()
##获取翻译的结果
>>> text = driver.find_element_by_class_name("tlid-translation")
>>> text.get_attribute('textContent')
'你好女孩'
示例
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Form</title>
<script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<h3>simple login form</h3>
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="text" id="inputEmail" placeholder="Email" name="email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="inputPassword" placeholder="Password" name="password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn">Sign in</button>
<a href="#">register</a>
</div>
</div>
</form>
</body>
</html>
操作示例:
from selenium import webdriver
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('form.html')
print file_path
dr.get(file_path)
# by id
dr.find_element_by_id('inputEmail').click()
执行后会将焦点定位到该输入框
# by name
dr.find_element_by_name('password').click()
执行后会将焦点定位到该输入框
# by tagname
print dr.find_element_by_tag_name('form').get_attribute('class')
获取form标签的class值
# by class_name
e = dr.find_element_by_class_name('controls')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', e)
sleep(1)
执行失败
# by link text
link = dr.find_element_by_link_text('register')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', link)
sleep(1)
执行失败
# by partial link text
link = dr.find_element_by_partial_link_text('reg')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', link)
sleep(1)
执行失败
# by css selector
div = dr.find_element_by_css_selector('.controls')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', div)
sleep(1)
# by xpath
dr.find_element_by_xpath('/html/body/form/div[3]/div/label/input').click()
sleep(2)
dr.quit()
定位一组对象
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Checkbox</title>
<script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<h3>checkbox</h3>
<div class="well">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="c1">checkbox1</label>
<div class="controls">
<input type="checkbox" id="c1" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c2">checkbox2</label>
<div class="controls">
<input type="checkbox" id="c2" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c3">checkbox3</label>
<div class="controls">
<input type="checkbox" id="c3" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="r">radio</label>
<div class="controls">
<input type="radio" id="r" />
</div>
</div>
</form>
</div>
</body>
</html>
批量操作
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
import os
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('checkbox.html')
dr.get(file_path)
# 选择所有的checkbox并全部勾上
checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]')
for checkbox in checkboxes:
checkbox.click()
time.sleep(1)
dr.refresh()
time.sleep(2)
# 打印当前页面上有多少个checkbox
print len(dr.find_elements_by_css_selector('input[type=checkbox]'))
# 选择页面上所有的input,然后从中过滤出所有的checkbox并勾选之
inputs = dr.find_elements_by_tag_name('input')
for input in inputs:
if input.get_attribute('type') == 'checkbox':
input.click()
time.sleep(1)
# 把页面上最后1个checkbox的勾给去掉
dr.find_elements_by_css_selector('input[type=checkbox]').pop().click()
time.sleep(1)
dr.quit()
send keys 按键模拟
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>send keys</title>
</head>
<body>
<h3>send keys</h3>
<div class="row-fluid">
<div class="span3">
<div class="well">
<label>A</label>
<textarea rows="10", cols="10" id="A">I think watir-webdriver is better than selenium-webdriver</textarea>
</div>
</div>
<div class="span3">
<div class="well">
<label>B</label>
<textarea rows="10", cols="10" id="B"></textarea>
</div>
</div>
</div>
</body>
</html>
操作:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('send_keys.html')
dr.get(file_path)
# copy content of A
dr.find_element_by_id('A').send_keys((Keys.CONTROL, 'a'))
dr.find_element_by_id('A').send_keys((Keys.CONTROL, 'x'))
sleep(1)
# paste to B
dr.find_element_by_id('B').send_keys((Keys.CONTROL, 'v'))
sleep(1)
# # send keys to A
dr.find_element_by_id('A').send_keys('watir', '-', 'webdriver', Keys.SPACE, 'is', Keys.SPACE, 'better')
sleep(2)
dr.quit()
处理button group
button group就是按钮组,将一组按钮排列在一起。处理这种对象的思路一般是先找到button group的包裹(wrapper)div,然后通过层级定位,用index或属性去定位更具体的按钮
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>button group</title>
<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function(){
$('.btn').click(function(){
alert($(this).text());
});
});
</script>
</head>
<body>
<h3>button group</h3>
<div class="row-fluid">
<div class="span3">
<div class="well">
<div class="btn-toolbar">
<div class="btn-group">
<div class="btn">first</div>
<div class="btn">second</div>
<div class="btn">third</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>
操作:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('button_group.html')
dr.get(file_path)
sleep(1)
# 定位text是second的按钮
buttons = dr.find_element_by_class_name('btn-group').find_elements_by_class_name('btn')
for btn in buttons:
if btn.text == 'second': print 'find second button'
sleep(1)
dr.quit()
####嵌套获取指定元素后
coding=utf-8
from selenium import webdriver
from time import sleep
import os
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('breadcrumb.html')
dr.get(file_path)
获得其父层级
anstors = dr.find_element_by_class_name('breadcrumb').find_elements_by_tag_name('a')
for ans in anstors:
print(ans.text)
sleep(1)
获取当前层级
由于页面上可能有很多class为active的元素
所以使用层级定位最为保险
print(dr.find_element_by_class_name('breadcrumb').find_element_by_class_name('active').text)
dr.quit()
## 获取对象的属性和该对象的文字内容
```HTML
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>attribute</title>
<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function(){
$('#tooltip').tooltip({"placement": "right"});
});
</script>
</head>
<body>
<h3>attribute</h3>
<div class="row-fluid">
<div class="span6">
<a id="tooltip" href="#" data-toggle="tooltip" title="watir-webdriver better than selenium-webdriver">hover to see tooltip</a>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>
草作:
# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('attribute.html')
dr.get(file_path)
link = dr.find_element_by_id('tooltip')
sleep(1)
# 获得tooltip的内容
print link.get_attribute('title')
# 获取该链接的text
print link.text
dr.quit()
获取测试对象的状态
在web自动化测试中,我们需要获取测试对象的四种状态
是否显示。使用element.is_displayed()方法;
是否存在。使用find_element_by_xxx方法,捕获其抛出的异常, 如果存在异常的话则可以确定该元素不存在;
是否被选中。一般是判断表单元素,比如radio或checkbox是否被选中。使用element.is_selected()方法;
是否enable,也就是是否是灰化状态。使用element.is_enabled()方法;
form的操作
表单对象的操作比较简单,只需要记住下面几点
使用send_keys方法往多行文本框和单行文本框赋值;
使用click方法选择checkbox
使用click方法选择radio
使用click方法点击button
使用click方法选择option,从而达到选中select下拉框中某个具体菜单项的效果
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>form</title>
<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function(){
$('input[type=submit]').click(function(){
alert('watir-webdriver is better than selenium webdriver');
});
});
</script>
</head>
<body>
<h3>form</h3>
<div class="row-fluid">
<div class="span6 well">
<form>
<fieldset>
<legend>Legend</legend>
<label class="checkbox">
<input type="checkbox"> Check me out
</label>
<label class="radio">
<input type="radio"> select me
</label>
<label class="select">
<select>
<option>0</option>
<option>1</option>
<option>2</option>
</select> select one item
</label>
<input type="submit" class="btn" value="submit" />
</fieldset>
</form>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>
操作:
# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('form.html')
dr.get(file_path)
# 选中checkbox
dr.find_element_by_css_selector('input[type=checkbox]').click()
sleep(1)
# 选中radio
dr.find_element_by_css_selector('input[type=radio]').click()
sleep(1)
# 选择下拉菜单中的最后一项
dr.find_element_by_tag_name('select').find_elements_by_tag_name('option')[-1].click()
sleep(1)
# 点击提交按钮
dr.find_element_by_css_selector('input[type=submit]').click()
sleep(10)
alert = dr.switch_to_alert()
print alert.text
alert.accept()
dr.quit()
COOKIE
webdriver可以读取并添加cookie。有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie的测试是无法通过白盒和集成测试完成的。
另外更加常见的一个场景是自动登陆。有很多系统的登陆信息都是保存在cookie里的,因此只要往cookie中添加正确的值就可以实现自动登陆了。什么图片验证码、登陆的用例就都是浮云了
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']
dr = webdriver.Chrome()
url = 'http://www.baidu.com'
dr.get(url)
print dr.get_cookies()
dr.delete_all_cookies()
dr.add_cookie({'name': 'BAIDUID', 'value': 'xxxxxx'})
dr.add_cookie({'name': 'BDUSS', 'value': 'xxxxxx'})
dr.get(url)
sleep(3)
dr.quit()