从selenium+python UI自动化开始
#!/usr/bin/env python
#!_*_coding:utf-8_*_
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.support.ui import Select
print "hello selenium"
#打开指定的网址或是页面
def openUrl():
print "打开百度首页"
driver = webdriver.Chrome()
driver.get("http://www.baidu.com/")
#driver.find_element_by_id('auto-id-1506309464597').send_keys('xxx')
sleep(3)
#driver.quit()
def mouseAction():
print "进入页面并模拟单击和双击操作并清除数据"
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://sahitest.com/demo/clicks.htm")
#sleep(20)
#ele = driver.find_element_by_xpath('/html/body/form/input[3]')
#sleep(2)
#ele.click()
#sleep(3)
#driver.quit()
#定位元素
ele = driver.find_element_by_xpath("//input[@value = 'click me']")
#鼠标单击,click(self, on_element)
ele.click()
sleep(3)
#鼠标双击,double_click(self, on_element),ActionChains(driver).double_click(button).perform()
ele1 = driver.find_element_by_xpath("//input[@value = 'dbl click me']")
ActionChains(driver).double_click(ele1).perform()
sleep(3)
#点击clear按键清除
ele2 = driver.find_element_by_xpath('/html/body/form/input[1]')
ele2.click()
sleep(3)
driver.quit()
#定位元素,输入内容
def keyboardAction():
print "输入storm搜索"
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.maximize_window()
#driver.find_element_by_id('kw').send_keys('storm')
# 输入框输入内容
driver.find_element_by_id("kw").send_keys("seleniumm")
sleep(5)
# 删除多输入的一个 m
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
sleep(5)
# 输入空格键+“教程”
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys("jiaocheng")
sleep(5)
# ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'a')
sleep(5)
# ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'x')
sleep(5)
# ctrl+v 粘贴内容到输入框
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'v')
sleep(5)
# 通过回车键来代替单击操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)
sleep(5)
driver.quit()
def switchLabs():
driver = webdriver.Chrome()
driver.maximize_window()
url = "http://www.baidu.com"
driver.get(url)
current_window_handle = driver.current_window_handle
print "当前handle", current_window_handle
#使用js打开新标签
js = "window.open('http://mail.163.com/')"
driver.execute_script(js)
all_window_handles = driver.window_handles
print "全部handle", all_window_handles
sleep(2)
for handle in all_window_handles:
if handle==current_window_handle: #如果当前的页面不是百度
driver.switch_to.window(handle)
sleep(2)
driver.quit()
#使用句柄index值去切换,默认打开的第一个窗口句柄是0,第二个是1
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
js = "window.open('http://www.sogou.com','_blank')"#js打开一个新窗口,打开搜狗
driver.execute_script(js) #Selenium执行js
#在这里我们手动将两个标签分离,模拟打开了两个窗口的效果
sleep(5)
handles = driver.window_handles
driver.switch_to.window(handles[0])
sleep(3)
driver.switch_to.window(handles[1])
sleep(3)
driver.quit()
#使用以下方式不能成功操作标签页面
#ActionChains(driver).key_down(Keys.CONTROL).send_keys("t").key_up(Keys.CONTROL).perform()
#ActionChains(driver).key_down(Keys.CONTROL).send_keys("w").key_up(Keys.CONTROL).perform()
def xpath_html():
driver = webdriver.Chrome()
driver.get(r'file:///home/tina/Downloads/Videoandread/selenium+python+UI/DOCTYPE.html')
#从根节点选取
eles = driver.find_element_by_xpath('/html')
print eles
print eles.tag_name
driver.quit()
#从目标节点下选取
driver = webdriver.Chrome()
driver.get(r'file:///home/tina/Downloads/Videoandread/selenium+python+UI/DOCTYPE.html')
eles = driver.find_elements_by_xpath('//head')
print eles
#print eles.tag_name
for ele in eles:
print ele.tag_name
driver.quit()
#定位一组元素和定位单个元素的方法类似,唯一的区别是再单词element后面多加了一个s
# . 选取当前节点,.. 选取父节点
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(r'file:///home/tina/Downloads/Videoandread/selenium+python+UI/DOCTYPE.html')
eles2 = driver.find_elements_by_xpath('//head/.')
eles3 = driver.find_elements_by_xpath('//head/..')
print "ele2:",eles2
for ele in eles2:
print "tag_name=",ele.tag_name
print "ele3:",eles3
for ele in eles3:
print "tag_name=",ele.tag_name
# @选取属性
eles4 = driver.find_elements_by_xpath('//*[@charest = "UTF-8"]')
for ele in eles4:
print ele.tag_name
sleep(3)
driver.quit()
def button11():
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://sahitest.com/demo/clicks.htm")
driver.find_element_by_xpath("/html/body/form/input[3]").click() #input type=button
sleep(3)
text = driver.find_element_by_name("t2").get_attribute('value')
if text == '[CLICK]':
print "pass"
driver.get("http://www.baidu.com")
driver.maximize_window()
driver.find_element_by_id('kw').send_keys('storm') #input type=submit
driver.find_element_by_id('su').click()
sleep(3)
#input type=radio
driver.get("http://sahitest.com/demo/clicks.htm")
ele = driver.find_element_by_xpath("/html/body/form/input[7]")
ele.click()
sleep(3)
if ele.is_selected():
print "pass"
sleep(3)
#复选框 input type=check box
driver.get("http://sahitest.com/demo/clicks.htm")
ele = driver.find_element_by_xpath('/html/body/ul/li/a/label/input')
ele.click()
sleep(2)
if ele.is_selected():
print('pass')
sleep(3)
ele.send_keys(Keys.SPACE) #自己可以手动实验下,按space键,可以选中或取消选中复选框
sleep(3)
driver.quit()
driver = webdriver.Chrome()
driver.get("http://sahitest.com/demo/keypress.htm")
eles = driver.find_elements_by_name('a')
for ele in eles:
ele.click()
sleep(2)
driver.quit()
#实现 UI 自动化测试-操作下拉列表
#可以通过index、value、visible_text三种方法操作
#select_by_index(0) 从0开始
#select_by_value() value是属性值
#select_by_visible_text('Fax') visible_text是下拉列表看到的值
def pull_down():
driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/selectTest.htm')
ele = driver.find_element_by_id('s1')
s1 = Select(ele)
s1.select_by_index(1) #通过index选中第一个选项
sleep(2)
s1.select_by_value('47') #通过value=47选中 cellphone
sleep(2)
s1.select_by_visible_text('Fax') #通过visible_text选中Fax
sleep(2)
print('all_selected_options: {}'.format(s1.all_selected_options)) #打印所有被选中的项
print('first_selected_options: {}'.format(s1.first_selected_option)) #打印第一个被选中的项
print('s1 is multiple: {}'.format(s1.is_multiple)) #这个select 是单选还是多选
print("ele text: {}".format(ele.text)) #显示所有下拉列表的选项visible text
print("ele attribute value: {}".format(ele.get_attribute('value'))) #显示选中的项的value属性值
driver.quit()
def table13():
#某个单元格中的值
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://sahitest.com/demo/tableTest.htm')
ele = driver.find_element_by_xpath('/html/body/table[1]/tbody/tr[1]/td[1]')
print (ele.text)
driver.quit()
def function():
#打印表格所有值
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://sahitest.com/demo/tableTest.htm')
table = driver.find_elements_by_xpath('/html/body/table[1]')
print table
print len(table)
table_rows = table[0].find_elements_by_tag_name('tr')
print "总行数:",len(table_rows)
table_cols = table_rows[0].find_elements_by_tag_name('td')
print "总列数:",len(table_cols)
for i in range(len(table_rows)):
for j in range(len(table_cols)):
cell = table_rows[i].find_elements_by_tag_name('td')[j]
print(cell.text)
#JavaScript 弹窗有三种:
#1、Alert
#2、Confirm
#3、Prompt
def JavaScriptP0P_windows():
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://sahitest.com/demo/promptTest.htm')
driver.implicitly_wait(20) #implicitly_wait()方法比 sleep() 更加智能,后者只能选择一个固定的时间的等待,前者可以在一个时间范围内智能的等待
driver.find_element_by_name('b1').click()
sleep(2)
driver.switch_to.alert.accept() #点确定
sleep(2)
driver.find_element_by_name('b1').click()
sleep(2)
driver.switch_to.alert.dismiss() #点取消
sleep(2)
driver.find_element_by_name('b1').click()
sleep(2)
print (driver.switch_to.alert.text) #打印弹出上的文字
driver.switch_to.alert.send_keys("hello strom") #输入发送文字
sleep(2)
driver.switch_to.alert.accept() #确定
sleep(3)
driver.quit()
#使用开发者工具是无法查看到弹框的元素的,也就是说alert是不属于网页DOM树的,假如你尝试在弹框上鼠标右键,是没有反应的。
if __name__ == "__main__":
openUrl()
mouseAction()
keyboardAction()
switchLabs()
xpath_html()
button11()
pull_down()
table13()function()
function()
JavaScriptP0P_windows()
备注:
使用driver.maximize_window(),最大化窗口时报错,大多是因为浏览器的驱动版本太低,影响脚本运行:
chromedriver驱动下载:https://sites.google.com/a/chromium.org/chromedriver/downloads
linux环境下chromedrive驱动安装:
将下载的文件移动到软件应该呆的目录去:
sudo mv chromedriver /usr/local/bin/chromedriver
改变用户执行的权限:
sudo chmod u+x,o+x /usr/local/bin/chromedriver
检验是否正常使用:
chromedriver --version
鼠标点击操作:
鼠标单击,click(self, on_element)
鼠标双击,double_click(self, on_element)
右键单击,context_click(self,on_element)
悬浮(移动鼠标到某元素),move_to_element(self, to_element)