之前一直是用java+selenium做自动化测试的,最近因为工作需要,需要用pyhton+selenium去实现,于是就赶驴上架,熟悉了一下python的语法和脚本的编写过程,下面是一个简单的脚本,但是基本涵盖了目前UI端比较常见的对象的识别和操作,比如输入框,按钮,下拉框,radio, checkbox, 链接, 鼠标脚本的操作以及脚本中js的使用。

上代码吧:

# coding = utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
import time driver=webdriver.Firefox()
driver.maximize_window()
driver.get('c:\\test.html')
def button():
btn=driver.find_element_by_id('user')
btn.send_keys('hello')
print('finish')
driver.close() def select():#se=driver.find_element_by_name('select')
#se.find_element_by_xpath('//option[@value="audi"]').click se=Select(driver.find_element_by_name('select'))
se.select_by_value('audi')
se.select_by_index(2)
time.sleep(5) def link():
lk=driver.find_element_by_class_name('baidu')
lktext=lk.get_attribute('text')
print(lktext)
lk.click() def checkbox():
ck=driver.find_element_by_name('checkbox1')
checked=ck.get_attribute('checked')
print(checked)
ck.click() def submit():
st=driver.find_element_by_class_name('button')
js='var st=document.getElementsByName("button");st.click()'
driver.execute_script(js) def js():
input=driver.find_element_by_id('user')
driver.execute_script('arguments[0].value="123"',input) def alert():
al=driver.find_element_by_class_name('alert')
al.click()
alert=driver.switch_to_alert()
print alert.__getattribute__('text')
time.sleep(5)
alert.accept()
#alert.dismiss() def switch():
#switch_to_window, switch_to_frame
#current_window_handle, window_handles
newwin=driver.find_element_by_class_name('open')
newwin.click()
time.sleep(3)
current=driver.current_window_handle
allhandle=driver.window_handles
for hd in allhandle:
print hd
driver.switch_to_window(allhandle[1]) time.sleep(3)
driver.switch_to_window(allhandle[0]) def action():
el=driver.find_element_by_class_name('over')
ActionChains(driver).move_to_element(el).perform()
time.sleep(3)
el2=driver.find_element_by_id('over')
#print(el2.get_attribute('text')) def wait():
driver.implicitly_wait(5)
driver.find_element_by_class_name('over') WebDriverWait(driver,5).until(lambda driver:driver.find_element_by_class_name('open').is_displayed()) if __name__=='__main__':
wait()

这个主要是对元素的操作,脚本中并未涉及到断言和报告的收集,后面补上这块的。

第一个UI脚本--python+selenium的更多相关文章

  1. 第二个UI脚本--Python+selenium之unittest+HTMLtestRunner及python的继承

    前面有一篇对于常见元素的识别和操作的python自动化脚本,这一篇就接着聊下python的类继承,已经它的第三款unittest框架,和报告收集包HTMLtestRunner的应用. 还是直接上代码吧 ...

  2. python+selenium封装UI自动化框架

    seleinum框架 框架的思想:  解决我们测试过程中的问题:大量的重复步骤,用自动化来实现    1)配置和程序的分离    2)测试数据和程序的分离    3)不懂编程的人员可以方便使用:使用的 ...

  3. Python selenium巧用Javascript脚本注入解决按钮点选问题

    前段时间,笔者忙于应付公司组织的雅思考试,白天.晚上但凡有空,笔者都是埋头伏案,啃剑桥雅思(剑4~剑12)的官方模拟题或者做着与雅思考试相关的准备工作,这个过程持续了40余天.最近总算鼓起勇气走进考场 ...

  4. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  5. 学霸笔记系列 - Python Selenium项目实战(一)—— 怎么去验证一个按钮是启用的(可点击)?

    Q: 使用 Python Selenium WebDriver 怎么去验证一个按钮是启用的(可点击)? A:Selenium WebDriver API 里面给出了解决方法is_enabled() 使 ...

  6. 编写第一个python selenium程序(二)

    上节介绍了如何搭建selenium 系统环境,那么本节来讲一下如何开始编写第一个自动化测试脚本. Selenium2.x 将浏览器原生的API封装成WebDriver API,可以直接操作浏览器页面里 ...

  7. python+selenium+unnitest写一个完整的登陆的验证

    import unittest from selenium import webdriver from time import sleep class lonInTest (unittest.Test ...

  8. 基于七牛Python SDK写的一个同步脚本

    需求背景 最近刚搭了个markdown静态博客,想把博客的图片放到云存储中. 经过调研觉得七牛可以满足我个人的需求,就选它了. 博客要引用图片就要先将图片上传到云上. 虽然七牛网站后台可以上传文件,但 ...

  9. python+selenium遍历某一个标签中的内容

    一.python+selenium遍历某一个标签中的内容 举个例子:我要获取列表标签<li></li>的内容 根据python+selenium定位到列表整体,使用for循环获 ...

随机推荐

  1. Partitioner

    partitioner 是map中的数据映射到不同的reduce时的根据.一般情况下,partitioner会根据数据的key来把数据平均分配给不同的reduce,同时保证相同的key分发到同一个re ...

  2. Database: Normal form

    refer to wikipedia--- 1NF(first normal form): 1. There's no top-to-bottom ordering to the rows. 2. T ...

  3. JavaScript —— 局部变量和全局变量

    JS的全局变量有3种声明方式: 1.Function 外 var v_myVar; 2.Function 内 v_myVar; 3.window.v_myVar window.v_myVar 全局变量 ...

  4. java-基础练习题3

    [程序3] 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因 ...

  5. [CCF2015.12]题解

    201512-1 数位之和 水题一个,取模除以10胡搞即可(不知道字符串为什么不行 #include <algorithm> #include <iostream> #incl ...

  6. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  7. JS获取系统的指定定年月日

    /** * 获取系统当前时间 */ function getNowYearMouth(){ var date=new Date; var nowYearMouth=date.getMonth()+1; ...

  8. android 开发如何做内存优化

    不少人认为JAVA程序,因为有垃圾回收机制,应该没有内存泄露.其实如果我 们一个程序中,已经不再使用某个对象,但是因为仍然有引用指向它,垃圾回收器就无法回收它,当然该对象占用的内存就无法被使用,这就造 ...

  9. linq to sql ,将var 类型转为 IList 类型

    public void SOHSelecting(int startRowIndex, int maximumRows, string sortExpression, string location) ...

  10. ajax连接数据库并操作数据库

    Response.Write("<script  type='text/javascript' language='javascript' >alert('用户名不能为空!请输入 ...