selenium 基础(一)
selenium安装
pip install selenium
selenium操作浏览器原理
早期selenium 1.0 用的selenium RC, 后来selenum2集合了selenium1.0 + webdriver,selenium RC被webdriver替换。通过webdriver,测试脚本(例如python)可以方便的通过API操作浏览器页面元素,包括打开,关闭,最大化,最小化,元素定位,元素单击等等等。但是selenium操作浏览器还需要一个驱动程序,不同的浏览器如filefox,chrome所需要的驱动程序不一样,就算是同款浏览器,因为浏览器内部提供的原生自动化接口API不同,也需要适配不同版本的驱动程序,不然就有可能出现调用接口失败的情况。
webdriver按照server-client经典设计模式设计,client可以理解为测试脚本,selenium支持多种语言(java,python,ruby,php等),server端可以理解为浏览器,client和server的通信根据the WebDriver Wire协议告诉服务端我们希望浏览器接下来做什么事情。
驱动下载
Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox: https://github.com/mozilla/geckodriver/releases
Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/
一个selenium简单的例子:(相应驱动要放在path环境变量中)
from selenium import webdriver
browser = webdriver.Chrome()
url = 'http://www.baidu.com'
browser.get(url)
浏览器基础操作
方法比较多,先整理一些,有需要后面再补充进来(有些参考了webdriver.py和webelement.py)
新建一个driver,初始化要操作的浏览器
driver = webdriver.Chrome()
访问url
driver.get(url)
最大化窗口
driver.maximize_window()
设置窗口宽,高
driver.set_window_size(width,height)
页面操作
browser.back()
browser.forward()
browser.refresh()
browser.close() 关闭当前窗口
browser.quit() 退出驱动并关闭每个关联窗口
页面信息
browser.title
browser.current_url
browser.current_window_handle 返回当前窗口句柄
driver.window_handles 所有窗口handle
frame切换
switch_to_frame()
switch_to_window()
对话框
switch_to_alert()
控件填写信息,也可以是文件上传
send_keys()
回车
send_keys(Keys.RETURN)
判断元素是否可见
is_displayed()
操作cookie
get_cookies()
get_cookie(name)
delete_cookie(name)
delete_all_cookies()
add_cookie(cookie_dict)
显式等待和隐式等待
显式等待会让WebDriver等待满足一定的条件以后再进一步的执行。 而隐式等待让Webdriver等待一定的时间后再才是查找某元素。
显式等待
WebDriverWait(driver, 10).untile(EC.visibility_of_element_located((By.CLASS_NAME, "logo_sogou")))
隐式等待
implicitly_wait(10)
屏幕截屏
save_screeshot(filepath)
元素查找
id查找
find_element_by_id("xxx")
name查找
find_element_by_name("xxx")
class查找
find_element_by_class_name("xxx")
css查找
find_element_by_css_selector('.s_ipt')
XPath查找
类似于xml定位一样,html的标签也可以用这类方式来查找,而且更健壮
find_element_by_xpath("/html/body/form[1]")
find_element_by_xpath("//form[1]") html页面中第一个form元素
find_element_by_xpath("//div[@id='search_ext']")
find_element_by_xpath("//div[@class='ipt_wrap']/span[1]")
find_element_by_xpath("//input[@name='continue'][@type='button']")
超链接查找
find_element_by_link_text('Continue') 完全匹配
find_element_by_partial_link_text('Conti') 部分匹配
一次查找返回多个元素(list)
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
参考文章:
http://selenium-python-zh.readthedocs.io/en/latest/index.html
selenium 基础(一)的更多相关文章
- 【Python爬虫】selenium基础用法
selenium 基础用法 阅读目录 初识selenium 基本使用 查找元素 元素互交操作 执行JavaScript 获取元素信息 等待 前进后退 Cookies 选项卡管理 异常处理 初识sele ...
- Selenium | 基础入门 | 截屏并保存于本地
可先参考 Selenium | 基础入门 | 利用Xpath寻找用户框 核心代码: //截屏操作 File srcFile = ((TakesScreenshot)driver).getScree ...
- web 自动化测试 selenium基础到应用(目录)
第一章 自动化测试前提及整体介绍 1-1功能测试和自动化测试的区别 1-2自动化测试流程有哪些 1-3自动化测试用例和手工用例的区别 1-4 自动化测试用例编写 1-5 selenium的优势以及 ...
- selenium基础-图形验证码
selenium基础-图形验证码 一.图形验证码作用 设计的初衷其实就是为了防自动化,防止一些人利用自动工具恶意攻击网站 二.图形验证码是由客户端生成还是由服务器端生成的? 图形验证码是由服务器端生成 ...
- selenium基础-跳过验证码
selenium基础-跳过验证码 一.方法 设置万能验证码或者屏蔽验证码(最常用的方法) 使用验证码识别工具识别验证码 通过selenium操作cookies 直接使用配置文件的webdriver 二 ...
- selenium基础(下拉菜单操作)
selenium基础(下拉菜单操作) 非select/option元素: 1.触发下拉列表出现 2.等待下拉列表中的元素出现,然后进行选择元素即可. select/option元素: 下拉框操作-Se ...
- selenium基础(脚本模块化)
selenium基础(脚本模块化)
- selenium基础(警告框的处理)
selenium基础(警告框的处理) 在webdriver中处理JavaScript所产生的的警告框有三种类型 alert confirm prompt 划转到警告框的方法是:driver.switc ...
- Python+Selenium基础入门及实践
Python+Selenium基础入门及实践 32018.08.29 11:21:52字数 3220阅读 23422 一.Selenium+Python环境搭建及配置 1.1 selenium 介绍 ...
随机推荐
- soj4538: ShouHuXueJie Problem DFS
类似八皇后,暴力深搜. 其实我觉得这题目叙述不是很好,如果答案为0呢,难道不输出? AC代码: #include<cstdio> #include<cstring> #incl ...
- XOR (莫队)
Time Limit: 2000 ms Memory Limit: 256 MB Description 给定一个含有n个整数的序列 a1, a2,..., an. 定义 f(x,x) = a[x ...
- kibana常用聚合查询DSL语句记录
-------- GET winlogbeat-2017.11.*/_search { "query": { "bool": { "must" ...
- Ansible自动化运维笔记2(Ansible的组件介绍)
1.Ansible Inventory (1)静态主机文件 默认的ansible invetory是/etc/hosts文件,可以通过ANSIBLE_HOSTS环境变量或者通过运行命令的时候加上-i ...
- 从 PHP 到 Java
* { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...
- mysql命令汇总
1.mysql新增.删除用户和权限分配 查看用户及权限 mysql>use mysql mysql>select * from user\G; 新增用户 mysql>insert i ...
- Android动态改变App在Launcher里面的icon
如果呆萌的产品童鞋让你动态更换App在Launcher里面的Icon,你怎么回答他,下文就提出一种实现该效果的方法. 原理1--activity-alias 在AndroidMainifest中,有两 ...
- FusionCharts 3D环饼图报错
1.在设计FusionCharts 3D环饼图时,出现错误,图显示不出来,具体错误如下图: 2.经过检查,发现声明的变量和下面引用的变量不一致 var doughnut2D = new FusionC ...
- PCI设备内存操作函数总结
1. ExAllocatePool() 函数说明: ExAllocatePool allocates pool memory of the specified type and returns a ...
- g++基本用法
用法:g++[选项]文件... g++编译流程: main.cxx #include <iostream> using namespace std; int main(void) { co ...