之前一直是用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. iOS:弹出窗控制器:UIPopoverController

    弹出窗控制器:UIPopoverController 截图:   实质:就是将内容控制器包装成popoverController的形式,然后在模态出来,必须给定指向目标(target.frame). ...

  2. Linux任务前后台的切换

    Shell支持作用控制,有以下命令实现前后台切换: 1. command& 让进程在后台运行 2. jobs 查看后台运行的进程 3. fg %n 让后台运行的进程n到前台来 4. bg %n ...

  3. java读写中文文件

    在用Java程序进行读写含中文的txt文件时,经常会出现读出或写入的内容会出现乱码.原因其实很简单,就是系统的编码和程序的编码采用了不同的编码格式.通常,假如自己不修改的话,windows自身采用的编 ...

  4. Web Server 使用WebClient 发送https请求 The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

    使用WebClient 发送https请求 使用WebClient发送请求时,并且是以https协议: WebClient webClient = new WebClient(); string re ...

  5. Win API 内存整理

    记得我的笔记本上曾经安装了一款名为内存整理大师的软件,当时觉得挺好用而且挺NB的,就是导致开机启动有点慢. 当时我就在想,内存整理是怎么实现的?不过那是水平实在是不怎么样,估计连windows程序的消 ...

  6. HDOJ ----Phone List

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. android-HttpClient上传信息(包括图片)到服务端

    需要下载apache公司下的HttpComponents项目下的HTTPCLIENT ----------地址为http://hc.apache.org/downloads.cgi 主要是用到了htt ...

  8. 二叉搜索树的两种实现(数组模拟,STL)

    书上实现: 二叉搜索数的特点:高效实现 插入一个数值,查询是否包含某个数值,删除某一个数值. 所有的节点都满足左子树上的所有节点都比自己的小,而右子树上的所有节点都比自己大的特点. 查询:如果当前数值 ...

  9. BZOJ 1507 Editor(块状链表)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...

  10. [POJ1631]Bridging signals (DP,二分优化)

    题目链接:http://poj.org/problem?id=1631 就是求一个LIS,但是范围太大(n≤40000),无法用常规O(n²)的朴素DP算法,这时需要优化. 新加一个数组s[]来维护长 ...