1.API讲解
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
driver. find_element_by_id( "kw" ) . send_keys( "龚俊" )
driver. find_element_by_id( "su" ) . click( )
time. sleep( 8 )
driver. close( )
driver. quit( )
2.元素的定位
元素的定位是自动化测试的核心,要操作一个对象,首先应该识别这个对象,一个对象就是一个人一样,他会有各种的特征(属性),如我们可以通过一个人的身份证号,姓名,或者他住哪个街道、楼层、门牌找到这个人。 注意:不管哪种方式,必须保证页面上的属性的唯一性。 webdriver提供了一系列的对象定位的方法,常用的有以下几种: id :id 是标识唯一的
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
driver. find_element_by_id( "kw" ) . send( "龚俊" )
// kw是百度那个框的的id
driver. find_element_by_id( "su" ) . click( )
// su是点击百度一下的id
time. sleep( 8 )
driver. quit( )
driver. find_element_by_name( "wd" ) . send_keys( "火焰蓝" )
driver. find_element_by_id( "su" ) . click( )
driver. find_element_by_xpath( "//*[@id='kw']" ) . send_keys( "山河令" )
driver. find_element_by_xpath( "//*[@id='su']" ) . click( )
driver. find_element_by_css_selector( "#kw" ) . send_keys( "人间百灵鸟" )
driver. find_element_by_css_selector( "#su" ) . click( )
name ,class name,link text,partial link text,tag name必须唯一的才能定位元素,id ,xpath,css selector 都可以唯一的定位到一个元素。
3. 操作测试对象
一般来说,webdriver中比较常用对象的方法有以下几个:
click点击对象 send_keys在对象上模拟按键输入
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
driver. find_element_by_id( "kw" ) . send_keys( "你好,火焰蓝" )
driver. find_element_by_id( "su" ) . click( )
time. sleep( 6 )
driver. quit( )
clear清除对象的内容,如果可以的话 submit提交表单
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
driver. find_element_by_id( "kw" ) . send_keys( "你好,火焰蓝" )
driver. find_element_by_id( "su" ) . click( )
time. sleep( 6 )
driver. find_element_by_id( "kw" ) . clear( )
time. sleep( 3 )
driver. find_element_by_id( "kw" ) . send_keys( "山河令" )
driver. find_element_by_id( "su" ) . submit( )
time. sleep( 6 )
driver. quit( )
text用于获取元素的文本信息
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
text = driver. find_element_by_id( "bottom_layer" ) . text
print ( text)
time. sleep( 6 )
driver. quit( )
5.添加等待
5.1 智能等待 vs 固定等待
固定等待:time.sleep(10) 必须等待10s 智能等待:driver.implicity_wait(10)需要加载的元素加载出来了,就立即停止等待,超过10s,报异常。
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
driver. find_element_by_id( "kw" ) . send_keys( "龚俊" )
driver. find_element_by_id( "su" ) . submit( )
driver. implicitly_wait( 10 )
driver. find_element_by_link_text( "龚俊的最新相关信息" ) . click( )
time. sleep( 6 )
driver. quit( )
6.打印title以及URL
6.1打印title
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
driver. find_element_by_id( "kw" ) . send_keys( "龚俊" )
driver. find_element_by_id( "su" ) . submit( )
time. sleep( 6 )
title= driver. title
print ( title)
time. sleep( 6 )
driver. quit( )
6.2打印URL
from selenium import webdriver
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
urll = driver. current_url
print ( urll)
driver. find_element_by_id( "kw" ) . send_keys( "龚俊" )
driver. find_element_by_id( "su" ) . submit( )
time. sleep( 6 )
url = driver. current_url
print ( url)
time. sleep( 6 )
driver. quit( )
7.浏览器的操作
7.1浏览器的最大化
driver. maximize_window( )
7.2设置浏览器的宽和高
driver. set_window_size( 400 , 1000 )
7.3 浏览器的前进和后退
driver. back( )
time. sleep( 6 )
driver. forward( )
8.浏览器的滚动条
driver. set_window_size( 400 , 1000 )
time. sleep( 3 )
js1 = "var q = document.documentElement.scrollTop = 10000"
driver. execute_script( js1)
time. sleep( 6 )
js2 = "var q = document.documentElement.scrollTop = 0"
driver. execute_script( js2)
9.键盘事件
9.1用键盘键控制界面
from selenium import webdriver
from selenium. webdriver. common. keys import Keys
import time
driver = webdriver. Chrome( )
driver. get( "http://127.0.0.1:88/zentao/user-login.html" )
driver. maximize_window( )
driver. find_element_by_id( "account" ) . send_keys( "admin" )
time. sleep( 3 )
driver. find_element_by_id( "account" ) . send_keys( Keys. TAB)
driver. find_element_by_name( "password" ) . send_keys( "Wjy12345678" )
time. sleep( 3 )
driver. find_element_by_id( "submit" ) . click( )
time. sleep( 6 )
driver. quit( )
9.2 回车
driver. find_element_by_name( "password" ) . send_keys( Keys. ENTER)
9.3 键盘组合键用法
from selenium import webdriver
from selenium. webdriver. common. keys import Keys
import time
driver = webdriver. Chrome( )
driver. get( "http://127.0.0.1:88/zentao/user-login.html" )
driver. maximize_window( )
driver. find_element_by_id( "account" ) . send_keys( "admin" )
time. sleep( 3 )
driver. find_element_by_id( "account" ) . send_keys( Keys. TAB)
password = driver. find_element_by_name( "password" )
password. send_keys( "Wjy123456" )
time. sleep( 3 )
password. send_keys( Keys. CONTROL, 'a' )
time. sleep( 3 )
password. send_keys( Keys. CONTROL, 'x' )
time. sleep( 3 )
password. send_keys( "Wjy12345678" )
driver. find_element_by_name( "password" ) . send_keys( Keys. ENTER)
time. sleep( 6 )
driver. quit( )
10.鼠标时间
from selenium import webdriver
from selenium. webdriver. common. action_chains import ActionChains
import time
driver = webdriver. Chrome( )
driver. get( "https://www.baidu.com/" )
driver. find_element_by_id( "kw" ) . send_keys( "龚俊---醉林珑" )
su = driver. find_element_by_id( "su" )
ActionChains( driver) . double_click( su) . perform( )
ActionChains( driver) . context_click( su) . perform( )
time. sleep( 7 )
driver. quit( )