转自
滑块:https://blog.csdn.net/stahuri/article/details/79400958
自动:https://www.cnblogs.com/LiNingKing/p/9378191.html
suoyou: https://www.cnblogs.com/z-x-y/category/1282473.html
一. selinum优势
页面级测试,模拟用户真实操作
强大library,支持页面元素各类操作
多浏览器支持(chrome,firefox,ie)
多语言支持(python,java,C#,php,ruby)
二. 需要安装软件
firefox firebug seleniumIDE
chrome chromedirver
ie iedriverserver
python2.7 selenium lib
三. Selenium IDE
对于初学者,可以先使用selenium IDE录制case,然后自动生成用例脚本
特点:工作为firefox插件,支持case录制、回放及代码导出
四. selenium入门
1.三种常用浏览器的启动方式
from selenium import webdriver
driver=webdriver.Chrome()
driver=webdirver.Firefox()
driver=webdriver.Ie()
2.关闭操作
driver.close()
关闭当前窗口
driver.quit()
关闭浏览器
3.selenium常用api
driver.current_url
driver.title
driver.page_source 源码
driver.name
driver.current_window_handle 当前窗口的句柄
4.跳转操作
driver.get("http://www.baidu.com")
driver.forward()
driver.back()
driver.refresh()
直到页面加载完全继续执行后面的程序,不等待ajax操作
配合time.sleep()方法使用
示例一:
#打开3个窗口,通过switch_to_window切换关闭窗口
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("http://www.baidu.com/")
print driver.window_handles
driver.switch_to_window(driver.window_handles[1])
driver.close()
实例二:
#打开百度首页,输入搜索内容并进行光标移动后回车
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("http://www.baidu.com/")
element=driver.find_element_by_id(‘kw‘)
element.clear()
element.send_keys(‘抗战胜利70周年‘.decode(‘gbk‘))
element.send_keys(Keys.ARROW_DOWN) #光标向下
element.send_keys(Keys.ENTER) #回车
5.常用元素操作
element.get_attribute(‘class‘)
element.is_displayed()
element.is_enabled()
是否可点击可输入等
element.is_selected()
element.location
坐标
element.parent
上一级
element.size
长宽
element.tag_name
返回标签名
element.text
返回文本
6.页面交互select操作
from selenium.webdriver.support.ui import Select
select=Select(driver.find_element_by_name(‘name‘))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)
select.deselect_all()
select.options
select.all_selected_options
示例三:
如上代码处理下拉框元素
方法一:
<pre class="brush:python;toolbar:false" style="font-family: Arial, Helvetica, sans-serif; padding: 5px; max-width: 680px !important; background-color: rgb(246, 246, 246); border: 1px dotted rgb(170, 170, 170); white-space: pre-wrap; overflow-wrap: break-word; color: rgb(63, 63, 63); font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from selenium import webdriver
driver=webdriver.Chrome()
driver.get(‘http://localhost/test.html‘)
element=driver.find_element_by_id(‘lang‘)
options=element.find_elements_by_tag_name(‘option‘)
for i in options:
print i.get_attribute(‘value‘)
for i in options:
print i.get_attribute(‘text‘)</pre>
方法二:
<pre class="brush:python;toolbar:false" style="font-family: Arial, Helvetica, sans-serif; padding: 5px; max-width: 680px !important; background-color: rgb(246, 246, 246); border: 1px dotted rgb(170, 170, 170); white-space: pre-wrap; overflow-wrap: break-word; color: rgb(63, 63, 63); font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver=webdriver.Chrome()
Select(driver.find_element_by_id(‘lang‘)).select_by_visible_text(‘简体‘.decode(‘gbk‘))</pre>
7.页面交互keys操作
from selenium.webdriver.common.keys import Keys
ALT
ARROW_DOWN /LEFT/RIGHT/UP
BACKSPACE
CONTROL
ENTER
ESCAPE
F1 /2/3/4/5...
SHIFT
APACE
TAB
8.页面交互wait操作--implicit wait
driver.implicitly_wait(10)
进行find操作时,等待固定秒数,成功退出计时
9.页面交互wait操作--explicit wait
按照一定条件执行wait操作
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
element=WebDriverWait(driver,1).until(expected_conditions.presence_of_elemrnt_located(By.ID,"idx"))
条件列表如下:
title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present
10.cookie操作
driver.get_cookies()
cookie={‘name‘:‘zz‘,‘value‘:18}
driver.add_cookie(cookie)
driver.get_cookie(‘zz‘)
driver.delete_cookie(‘zz‘)
11.js操作
js=‘alert("hello")‘
driver.execute_script(js)
js=‘console.log("hello")‘
driver.excute_script(js)
五. selenium元素定位方法
1.定位单元素
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selecror
2.定位多元素
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selecror
3.通用方法
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH,‘//button[text()="some text"]‘)
driver.find_elements(By.XPATH,‘//button‘)