高级金牌讲师_selenium实战
一、定位文本框,密码框
a. 先使用web元素定位方法定位文本框,密码框,再使用send_keys()方法进行文本,
密码输入
案例:
网站:http://cms.duoceshi.cn/cms/manage/login.do
案例:
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj="http://cms.duoceshi.cn/cms/manage/login.do"
d.get(lj)
sleep(2)
d.maximize_window()
d.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
d.find_element_by_id('loginPwd').send_keys("123456")
二、定位普通按钮,链接,隐藏框
案例1:按钮
网站:http://cms.duoceshi.cn/cms/manage/login.do
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj="http://cms.duoceshi.cn/cms/manage/login.do"
d.get(lj)
sleep(2)
d.maximize_window()
d.find_element_by_id("userAccount").send_keys("admin")#账号
sleep(2)
d.find_element_by_id('loginPwd').send_keys("123456") #密码
sleep(2)
d.find_element_by_id("loginBtn").click() #点击登录
案例2:链接
网站:https://www.baidu.com/
from selenium import webdriver:
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/"
d.get(lj)
sleep(2)
d.find_element_by_link_text("hao123").click()
案例3:隐藏框
场景1:悬停
快捷键:ctrl+shift+c 查看隐藏元素
备注:
导入:from selenium.webdriver.common.action_chains import ActionChains 模块
ActionChains(对象).move_to_element(位置).perform()
案例:悬停
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/"
d.get(lj)
sleep(2)
gd=d.find_element_by_link_text("更多")
ActionChains(d).move_to_element(gd).perform()
sleep(2)
d.find_element_by_link_text("百科").click()
场景2:百度中设置的的隐藏框
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/"
d.get(lj)
sleep(2)
d.find_element_by_id("s-usersetting-top").click()
sleep(2)
d.find_element_by_link_text("高级搜索").click()
======================================================
获取元素的文本
text 文本
方法:先使用web元素定位方法定位元素,再使用text获取文本
案例:百度:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/"
d.get(lj)
sleep(2)
wb=d.find_element_by_id("s-top-loginbtn").text
# print(wb)
if wb=="登录":
print("ok")
else:
print('no')
========================================
title 获取窗口title
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/"
d.get(lj)
sleep(2)
print(d.title) #百度一下,你就知道
lj1="http://cms.duoceshi.cn/cms/manage/login.do"
d.get(lj1)
sleep(2)
print(d.title) #过期更新
========================================
判断语句:
(1)if语句
无论是对,还是错,我们都有结果;
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/"
d.get(lj)
wb=d.find_element_by_id("s-top-loginbtn").text
# print(wb)
if wb=="登录111":
print("ok")
else:
print('no')
(2)assert 语句
错误就报错,对了就继续执行语句
案例:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/"
d.get(lj)
wb=d.find_element_by_id("s-top-loginbtn").text
# assert wb=="登录"#正确的直接执行下一步
assert wb=="不登录" #错误直接报错
print(wb)
=======================================
切换窗口
场景一:同一个浏览器,同一个窗口
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/" #百度
d.get(lj)
sleep(2)
lj1="https://www.jd.com/" #京东
d.get(lj1)
print(d.title) #京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!
场景2:同一个浏览器,不同的窗口,抬头在第一个窗口
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/" #百度
d.get(lj)
sleep(2)
lj2="window.open('https://www.jd.com/')"
d.execute_script(lj2)
sleep(2)
print(d.title) #百度一下,你就知道
场景3:两个窗口 通过切换句柄来切换窗口(0,1)
案例:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/" #百度
d.get(lj)
sleep(2)
lj2="window.open('https://www.jd.com/')"
d.execute_script(lj2)
sleep(2)
print(d.title) #百度一下,你就知道
jb=d.current_window_handle #获取当前句柄
print(jb) #CDwindow-A07BEFCE50143218D8B0CA73029B6017
syjb=d.window_handles #所有的句柄
print(syjb) #['CDwindow-A07BEFCE50143218D8B0CA73029B6017', 'CDwindow-09751853A8EE8EB193CF676122E6CD64']
d.switch_to.window(syjb[1])
sleep(2)
print(d.title) #京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!
d.find_element_by_link_text("你好,请登录").click()
案例4:三个窗口 通过切换句柄来切换窗口(0,2,1)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/" #百度
d.get(lj)
sleep(2)
lj2="window.open('https://www.jd.com/')"
d.execute_script(lj2)
sleep(2)
lj3="window.open('https://i.cnblogs.com/posts')"
d.execute_script(lj3)
print(d.title) #百度一下,你就知道
jb=d.current_window_handle #获取当前句柄
print(jb) #CDwindow-A07BEFCE50143218D8B0CA73029B6017
syjb=d.window_handles #所有的句柄
print(syjb) #['CDwindow-A07BEFCE50143218D8B0CA73029B6017', 'CDwindow-09751853A8EE8EB193CF676122E6CD64']
d.switch_to.window(syjb[2])
sleep(2)
print(d.title) #京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!
# d.find_element_by_link_text("你好,请登录").click()
案例5:通过if语句判断 title 来切换窗口
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
d=webdriver.Chrome()
lj="https://www.baidu.com/" #百度
d.get(lj)
sleep(2)
lj2="window.open('https://www.jd.com/')"
d.execute_script(lj2)
sleep(2)
lj3="window.open('https://i.cnblogs.com/posts')"
d.execute_script(lj3)
print(d.title) #百度一下,你就知道
jb=d.current_window_handle #获取当前句柄
print(jb) #CDwindow-A07BEFCE50143218D8B0CA73029B6017
syjb=d.window_handles #所有的句柄
print(syjb) #['CDwindow-A07BEFCE50143218D8B0CA73029B6017', 'CDwindow-09751853A8EE8EB193CF676122E6CD64']
for i in syjb:
d.switch_to.window(i)
if "博客园" in d.title:
break
print(d.title)
======================================
获取下拉框选项
引入类:Select()
from selenium.webdriver.support.select import Select
实战网址:https://www.ctrip.com/
1)select_by_index() #通过下标定位下拉选项
2)select_by_value() #通过value属性值定位下拉选项
3)select_by_visible_text() #通过下拉文本内容内容
案例:
路径:
file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/table.html
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
d=webdriver.Chrome()
lj2="file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/table.html"
d.get(lj2)
sleep(2)
wz=d.find_element_by_xpath("/html/body/form/table/tbody/tr[7]/td[2]/select")
# Select(wz).select_by_visible_text("清华大学")#文本值定位
Select(wz).select_by_index(2) #根据索引定位
Select(wz).select_by_value("1") #根据calue定位
======================================
15、弹框处理
三种弹框的处理方法:警告型,确认型,输入型弹框
t = driver.switch_to.alert() #切换进入alert()弹框
t.text() # 获取弹框的上面的文本
t.accept() #点击确定按钮
t.dismiss() #点击取消按钮
send_keys() #输入型弹框可以在弹框上面进行文本输入
案例:
(1)路径:alert弹框file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/alert%E5%BC%B9%E6%A1%86.html
案例:from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj="file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/alert%E5%BC%B9%E6%A1%86.html"
d.get(lj)
sleep(2)
tk=d.switch_to.alert
sleep(2)
tk.accept()# 确定
# tk.dismiss()# 取消
(2)确认型弹框
:路径file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/enter.html
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj="file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/enter.html"
d.get(lj)
sleep(2)
d.find_element_by_class_name("alert").click()
sleep(2)
tk=d.switch_to.alert
sleep(2)
tk.accept()# 确定
# tk.dismiss()# 取消
(3)输入型弹框
路径:
file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/prompt.html
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj="file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/prompt.html"
d.get(lj)
sleep(2)
d.find_element_by_class_name("alert").click()
sleep(2)
tk=d.switch_to.alert
sleep(2)
tk.send_keys("dcs")
sleep(2)
tk.accept()# 确定
(4)上传文件型弹框
file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/upload_file.html
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj="file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/upload_file.html"
d.get(lj)
sleep(2)
d.find_element_by_id("file").send_keys(r"D:\f\yy\ss.xlsx")
=====================================
iframe框处理
定位的元素在iframe标签里面,需要先进入iframe,再进行元素定位
进入iframe框方法:switch_to.frame()
退出iframe框方法:switch_to.default_content()
案例1:
路径:https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&state=DC2EB102167EB90DAC5CF600E5C3E26BE8DB735314DE524CBA25757F99D656D8CF7CDC1BAF60ABBABD53344020B76C27&client_id=100273020&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fview%3Dnull%26uuid%3D246fff3fc6dc46daa0a1cf09e8e61a4d
路径:https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&state=DC2EB102167EB90DAC5CF600E5C3E26BE8DB735314DE524CBA25757F99D656D8CF7CDC1BAF60ABBABD53344020B76C27&client_id=100273020&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fview%3Dnull%26uuid%3D246fff3fc6dc46daa0a1cf09e8e61a4d
案例:
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&state=DC2EB102167EB90DAC5CF600E5C3E26BE8DB735314DE524CBA25757F99D656D8CF7CDC1BAF60ABBABD53344020B76C27&client_id=100273020&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fview%3Dnull%26uuid%3D246fff3fc6dc46daa0a1cf09e8e61a4d"
d.get(lj)
sleep(2)
kdz=d.find_element_by_id("ptlogin_iframe")
d.switch_to.frame(kdz) #进框
sleep(2)
d.find_element_by_link_text("帐号密码登录").click()
d.switch_to.default_content() #退框
sleep(2)
d.find_element_by_link_text("服务协议").click()
=====================================
滚动条是由js代码编写的
方法:execute_script()
代码如下:
js1=window.scrollTo(0,20000) #向下滚动20000px
driver.execute_script(js1)
js2=window.scrollTo(0,0) #从底部滚动到顶部
driver.execute_script(js2)
案例:
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj2="https://www.jd.com/"
d.get(lj2)
d.maximize_window()
sleep(3)
js1='window.scrollTo(0,20000)'
d.execute_script(js1)
sleep(10)
js2='window.scrollTo(0,0)'
d.execute_script(js2)
方法二:js方法滑动
var d=document.documentElement.scrollTop=10000
js2='var d=document.documentElement.scrollTop=0'
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj2="https://www.jd.com/"
d.get(lj2)
d.maximize_window()
sleep(3)
js1='var d=document.documentElement.scrollTop=10000'
d.execute_script(js1)
sleep(10)
js2='var d=document.documentElement.scrollTop=0'
d.execute_script(js2)
==========================
通过循环滑动屏幕
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
lj2="https://www.jd.com/"
d.get(lj2)
d.maximize_window()
sleep(3)
sum=0
while sum<=10000:
sum+=1000
js1='var d=document.documentElement.scrollTop='+str(sum)
d.execute_script(js1)
sleep(10)
==================================================
Keys类
from selenium.webdriver.common.keys import Keys
删除单个字符:Keys.BACK_SPACE
全选:Keys.CONTROL,'a'
剪切:Keys.CONTROL,'x'
粘贴:Keys.CONTROL,'v'
点击确定:Keys.ENTER
进阶:可以把相同的步骤封装在一个函数里面
案例:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
d=webdriver.Chrome()
lj2="https://www.baidu.com/"
d.get(lj2)
d.maximize_window()
sleep(2)
d.find_element_by_id("kw").send_keys("dcs") #输入
sleep(2)
d.find_element_by_id("kw").send_keys(Keys.CONTROL,"a")#全选
sleep(2)
d.find_element_by_id("kw").send_keys(Keys.CONTROL,"x")#剪切
sleep(2)
d.find_element_by_id("kw").send_keys(Keys.CONTROL,"v")#粘贴
sleep(2)
d.find_element_by_id("kw").send_keys(Keys.ENTER)#确认
==================================================
封装
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
d=webdriver.Chrome()
lj2="https://www.baidu.com/"
d.get(lj2)
d.maximize_window()
sleep(2)
def ff(lj,*v):
d.find_element_by_id(lj).send_keys(*v)
sleep(2)
if __name__ == '__main__':
ff('kw','dcs')
ff('kw',(Keys.CONTROL,"a"))
ff('kw', (Keys.CONTROL, "x"))
ff('kw', (Keys.CONTROL, "v"))
ff('kw', (Keys.ENTER))
==================================================