Selenium基于Python web自动化基础一 -- 基础汇总及简单操作
Selenium是UI层WEB端的自动化测试框架,也是目前市面上比较流行的自动化测试框架。
ui层自动化测试本质是什么?
模拟用户的真实操作行为。
基础汇总:
导入所需要的模块
from selenium import webdriver
import time 生成一个driver变量
driver = webdriver.Firefox() #浏览器最大化
# driver.maximize_window()
#自定义浏览器的宽和高
# driver.set_window_size(,) url = "http://www.cnblogs.com/hanxiaobei/"
打开url链接
driver.get(url)
time.sleep()
打印打开页面的title
print("网站title",driver.title) #截图并保存为1.png
driver.get_screenshot_as_file("1.png") #刷新当前页面
driver.refresh() time.sleep()
driver.get("http://www.baidu.com")
print("网站的title",driver.title) driver.back() #后退
driver.forward()#前进 driver.close() #关闭页面,进程还在
# driver.quit() #关闭浏览器,进程杀死
定位元素的7种方式:
# driver.find_element_by_id("kw").send_keys("selenium")
# driver.find_element_by_name("wd").send_keys("selenium")
# driver.find_element_by_class_name("s_ipt").send_keys("selenium")
# driver.find_element_by_xpath(".//*[@id='kw']").send_keys("selenium")
#定位超链接:第一种是精确匹配,第二种是模糊匹配
# driver.find_element_by_link_text("新闻").click()
# driver.find_element_by_partial_link_text("我是那个").click()
# driver.find_element_by_css_selector(".s_ipt").send_keys("selenium")
定位元素的另一种写法:
driver.find_element("id","kw").send_keys("selenium")
获取元素文本内容
blogname = driver.find_element_by_class_name('blogName').text
print("获取的文本内容是:",blogname)
xpath定位总结:
1、绝对路径:driver.find_element_by_xpath("html/body/a").click()
2、//标签名[@属性名=属性值]:
driver.find_element_by_xpath("//a[@href='http://www.baidu.com/s?wd=测试']").click()
driver.find_element_by_xpath("//*[@href='http://www.baidu.com/s?wd=测试']").click() 3、contains、start-with:
driver.find_element_by_xpath("//a[contains(@href,'测试')]").click() #包含
driver.find_element_by_xpath("//a[starts-with(@href,'in')]").click() #以什么开头
#//div[starts-with(@id,'in')] 选择id属性以in开头的 4、利用两个元素属性进行唯一定位:and、or
driver.find_element_by_xpath("//*[@id='kw' and @name='wd']")
Selenium基于Python web自动化基础一 -- 基础汇总及简单操作的更多相关文章
- Selenium基于Python web自动化基础二 -- 免登录、等待及unittest单元测试框架
一.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...
- Selenium基于Python web自动化测试框架 -- PO
关于selenium测试框架首先想到的就是PO模型,简单说下PO模型 PO模型的概念和理解: PO就是一个设计思想,将代码以页面为单位进行组织,针对这个页面上的所有信息.相关操作都放到一个类中,从而使 ...
- 【Selenium04篇】python+selenium实现Web自动化:文件上传,Cookie操作,调用 JavaScript,窗口截图
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第四篇博 ...
- Python Web 应用:WSGI基础
在Django,Flask,Bottle和其他一切Python web 框架底层的是Web Server Gateway Interface,简称WSGI.WSGI对Python来说就像 Servle ...
- python + web自动化,点击不生效,提示“selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (117, 674)”
前言: 在做web自动化时,遇到一个缩放了浏览器比例的操作,从100%缩小到80%,再进行点击的时候,弹出报错信息,无法点击 selenium.common.exceptions.ElementCli ...
- Selenium基于Python 进行 web 自动化测试
配置使用环境 下载相应的浏览器驱动, Firefox 是默认的 本文以 chrome 为主 ,放在scripts目录下ChromeDriver 官方下载地址 : 所有版本的 ChromeDriver ...
- 初识TPOT:一个基于Python的自动化机器学习开发工具
1. TPOT介绍 一般来讲,创建一个机器学习模型需要经历以下几步: 数据预处理 特征工程 模型选择 超参数调整 模型保存 本文介绍一个基于遗传算法的快速模型选择及调参的方法,TPOT:一种基于Pyt ...
- 【Python web自动化】之读取配置文件参数,利用cookie返回值进行跳过验证码进行登录操作
当进行Python的Web自动化时,会涉及到验证码问题,该如何跳过执行呢,下面请看代码: 1.首先新建配置文件*.ini格式 config.ini [db] #基础地址: baseurl = http ...
- 基于Python项目的Redis缓存消耗内存数据简单分析(附详细操作步骤)
目录 1 准备工作 2 具体实施 1 准备工作 什么是Redis? Redis:一个高性能的key-value数据库.支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使 ...
随机推荐
- neutron dhcp
neutron dhcp 采用dnsmasq服务来实现.和传统的 dhcp 一样, dhcp请求也分为4步 The client sends a discover ("I'm a clien ...
- mybatis最重要的mapper文件书写
1.MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap. 也只有在mapper的select标签中,才会指定resultMap属性的值,其 ...
- Linux纯Shell实现DNSPod动态域名
http://www.anrip.com/post/872 开发背景: 公司有台嵌入式拨号上网设备,内置busybox和完整wget命令(支持https协议),但没有curl.python.ruby. ...
- ubuntu下进行ssh
ubuntu下进行ssh 一, 介绍 SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立 ...
- 自已实现一个UI库
[2014年写一个UI库时写的几个文章,公布出来] 几年前的一个嵌入式的UI开发,使自己有机会接触到了UI的一些底层知识,尽管之前也开发过非常多Windows下的信息应用系统,也做非常多的界面开发,但 ...
- mysql 经常使用命令整理总结
#改动字段类型 alter table `table_name` modify column ip varchar(50); #添加字段 alter table `table_name` add ip ...
- Oracle index unusable和invisible的差别
摘录自11g的官方文档: UNUSABLE Specify UNUSABLE to mark the index or index partition(s) or index subpart ...
- JDBC基本应用
首先我们来看一下JDBC操作数据的核心: Connection 应用程序与数据库之间的桥梁 数据库驱动程序是构建桥梁的基石和材料 DriverManager类是基石和材料的管理员 Statement ...
- initial ram filesystem
1 制作最简单的initramfs 1.1 创建init #include <stdio.h> int main() { printf ("\n"); printf ( ...
- 简易 DBUtil 封装
Dao包结构图: 1.首先连接数据库 package com.util.db; import java.sql.Connection; import java.sql.DriverManager; i ...