Selenium 从安装到使用
一、简介
Selenium是一个工具集。
在2.0之前主要包括四个工具:
Selenium IDE(火狐的一个插件,可以快速的实现录制和回放脚本)
Selenium RC (Selenium 1.0的核心,实现对浏览器的控制但是有很多限制)
Selenium Webdriver (Selenium 2.0 以后的核心,用于控制浏览器)
Selenium Grid(支持脚本在多台机器上并行执行脚本,提升执行效率)
3.0版本,由以下3核心组件组成:
Selenium IDE
Selenium Grid
Selenium Webdriver
二、安装
接下来说一下安装步骤(Python版本的)
- 首先你要安装一个Python
- 然后你就可以通过安装第三方库的方式安装Selenium了,比如使用 pip install selenium 安装,或者其他的第三方库的安装方式也可以
- 最后你需要下载对应的浏览器版本驱动就可以了,最好是将驱动放在你的Python安装路径下,相关驱动下载链接如下:
Firefox:https://github.com/mozilla/geckodriver/releases
IE:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Chrome:https://sites.google.com/a/chromium.org/chromedriver/
4、运行如下代码(谷歌浏览器):
from selenium import webdriver #导webdriver |
如果可以打开谷歌浏览器并打开到百度搜索页面就说明安装成功了
补充:selenium的运行原理(如下图)
- 自动化程序调用Selenium 客户端库函数(比如点击按钮元素)
- 客户端库会发送Selenium 命令 给浏览器的驱动程序
- 浏览器驱动程序接收到命令后 ,驱动浏览器去执行命令
- 浏览器执行命令
- 浏览器驱动程序获取命令执行的结果,返回给我们自动化程序
- 自动化程序对返回结果进行处理
- 使用
3.1元素的定位(看黑板考试要考的重点)
3.1.1、CSS选择器
1)通过伪类名、id、标签名定位
2)通过元素之前嵌套关系
3)通过属性
4)通过父子关系
5)元素状态
3.1.2、Xpath
1)通过绝对路径定位元素(不推荐使用):
find_element_by_xpath("/html/body/div/form/input"); |
2)通过相对路径定位元素:
find_element_by_xpath("//div/input");#选取所有的div下面的input |
3)使用索引定位元素:
find_element_by_xpath("//input[4]"); #全页面查找,找到第4个input 返回页面对象 find_elements_by_xpath("//input")[3]; #全页面查找所有的input,取第4个input 定位一组元素,返回列表 |
4)使用xpath及属性值定位元素:
find_element_by_xpath("//input[@id='kw']"); # 全文查找id = kw的这个input标签 |
5)部分属性值匹配:
find_element_by_xpath("//*[contains(@href, 'news.baidu')]") 匹配包含属性的值 #全文查找所有标签,href属性包含news.baidu的标签
find_elements_by_xpath(" //*[starts-with(@href,'https')]")匹配开始字段,ends-with方法不可用 #全文查找所有标签,href属性以https开头的标签 |
3.2 操作(看黑板考试要考的重点)
3.2.1 、浏览器操作
get(“URL”) #打开指定的地址
back() #后退
forward() #前进
refresh() #刷新
maxmize_window() #最大化浏览器
minimize_window() #最小化浏览器
set_window_size(x,y) #设置浏览器大小
quit() #关闭浏览器
close() #关闭窗口
3.2.2 、元素定位
find_element_by_id() #通过id属性值
find_element_by_name() #通过name属性值
find_element_by_class_name() #通过class属性值
find_element_by_link_text() #通过超链接文本内容
find_element_by_partial_link_text() #通过部分超链接文本内容
find_element_by_css_selector() #通过css定位
find_element_by_xpath() #通过xpath定位
find_element_by_tag_name() #通过标签名
定位一组元素除了id的没有其他的方法可以将element改为elements
By定位
from selenium.webdriver.common.by import By
Find_element(By.ID,”ID名”)
...与上面方法相同,将By.ID换成相应的方法即可
层级定位:
在一个element对象下去定位element
3.2.3 、界面元素(操作&获取)
操作:
click() #点击
clear() #清空
submit() #表单提交
send_keys() #输入内容
获取:
size #获取元素尺寸
title #获取页面标题
text #获取元素文本
current_url #获取当前url
get_attribute(“属性”) #获取元素属性值
3.2.4 、键盘操作
一般和send_keys()连用
from selenium.webdriver.common.keys import Keys
如: send_keys(Keys.Back_SPACE) #回退键
方法还有以下:
Keys.SPACE #空格键
Keys.TAB #制表键
Keys.Enter #回车键
send_keys(Keys.CONTROL,”a”) #全选
send_keys(Keys.CONTROL,”c”) #复制
send_keys(Keys.CONTROL,”v”) #粘贴
send_keys(Keys.CONTROL,”x”) #剪切
3.2.5 、鼠标操作
导入模块
from selenium.webdriver.common.action_chains form ActionChains
如:ActionChains(driver).drag_and_drop(aa,bb).perform()#把aa拖动到bb里面去 perform() 执行 ActionChains 动作链
click() #单击
context_click() #右击
double_click() #双击
click_and_hold() #左击悬停
move_to_element() #鼠标悬停
drag_and_drop() #拖动
3.2.6 、多窗口
current_window_handle 获取当前窗口handle
window_handles 获取所有窗口handle,得到一个列表
switch_to.window() 切换窗口
3.2.7 、多表单
switch_to.frame() #切换到一个frame或iframe中
switch_to.parent_frame() #切换到上一级
Switch_to.default_frame() #切换到最外层
3.2.8 、弹出框
使用switch_to_alert()定位到弹出框 包括alert、confirm、prompt
在使用相关操作
text #获取弹出框文本内容
accept() #点击确定
dismiss() #点击取消
send_keys() #输入内容 只在prompt存在
3.2.9 、下拉框
导入:
from selenium.webdriver.support.select import Select
#三种选中方法
select_by_visible_text()
select_by_index()
select_by_value()
#四种取消选中方法
deselect_all()
deselect_by_index()
deselect_by_value()
deselect_by_visible_text()
#三种返回方法
options
all_selected_options
first_selected_option
#判断是否是多选框
is_multiple
3.2.10 、等待
强制等待:
导入:from time import sleep
sleep(3) #强制等待三秒
隐式等待:
implicitly_wait(max_time) #设置最大等待时间
显示等待
导入:
from selenium.webdriver.support import WebDriverWait from selenium.webdriver.support import expected_conditions WebDriverWait(driver,timeout,check_time,ignored_exceptions).until(expected_conditions.presence_of_element_located(locator),message)
|
driver: WebDriver的驱动程序(Ie, Firefox, Chrome ),既是传入WebDriver实例,即driver。
timeout: 超时时间,等待的最长时间
check_time: 调用until中方法的间隔时间,默认是0.5秒
ignored_exceptions: 超时后的异常信息,默认情况下抛NoSuchElementException异常。(没有找到元素)。
until():
method: 在等待期间,每隔一段时间,调用这个方法,直到返回值为True
message: 如果超时,抛出TimeoutException,将message传入异常
presence_of_element_located()判断元素是否存在
3.2.11 、文件上传
普通文件上传,将文件路径放在input标签中,通过from表单提交
3.2.12 、截图
方法一:
get_screenshot_as_file('imgname')
方法二:
安装第三方库PIL
导入: from PIL import ImageGrab
方法2
im = ImageGrab.grab()
im.save(r'E:\getPicture\alert2.png')