框的定位、滚动条、文本、断言、快捷键、实战项目编写
一、文本框、密码框、输入、点击、 按钮
1.id 账号密码:导入模块,驱动谷歌,进入网页,通过id索引find_element_by_id输入字符。
通过id点击click,id在开发者模式的小箭头找
网址:http://cms.duoceshi.cn/manage/login.do
案例:
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
dx.get("http://cms.duoceshi.cn/manage/login.do")
dx.maximize_window()
sleep(2)
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
2、链接 链接是by_link_text然后点击。
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
dx.find_element_by_link_text("hao123").click()
3、隐藏框
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wz=dx.find_element_by_link_text("更多") #通过网页定位更多选项ActionChains(dx).move_to_element(wz).perform() #鼠标悬停
dx.find_element_by_xpath('//*[@id="s-top-more"]/div[2]/a/div').click()
#通过xpath id定位,点击
ctrl+shift+c键查看标签
鼠标悬停:ActionChains用法(move_to_element()/drag_and_drop())_actionchains move-CSDN博客
selenium——鼠标操作ActionChains:点击、滑动、拖动_actionchains将某个元素向左右滑动-CSDN博客
更多案例:设置的隐藏框
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wz=dx.find_element_by_id("s-usersetting-top") #通过id定位设置选项
ActionChains(dx).move_to_element(wz).perform() #鼠标悬停
dx.find_element_by_link_text("关闭预测").click() #点击网页定位关闭预测选项
4、获取元素的文本(text)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wb=dx.find_element_by_link_text("登录").text
if wb=="登录":
print("断言成功")
else:
print("断言失败")
5、断言(if 、assert)
(1)if断言:
(2)assert 断言
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wb=dx.find_element_by_link_text("登录").text
assert wb=="登录qq"
print(1)
常用的场景:日志
selenium提供了三种模式的断言:assert 、verify、waitfor_selenium verify-CSDN博客
6、获取窗口的大小(get_window_size())
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
dx.get("http://baidu.com")
sleep(2)
a=dx.get_window_size() #{'width': 945, 'height': 1012}
print(a)
二、切换窗口
(1) 获取当前句柄(current_window_handle)
获取所有的句柄 (window_handles)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("http://jd.com")'
dx.execute_script(w) #再打开一个京东网页
print(dx.title)
jb=dx.current_window_handle #获取当前窗口句柄
print(jb)
jbs=dx.window_handles #获取所有窗口句柄
print(jbs)
(2)通过句柄切换窗口(两个窗口的排序0,1)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("https://www.jd.com/")'
dx.execute_script(w)
print(dx.title)
jb=dx.current_window_handle #获取当前窗口句柄
print(jb) #CDwindow-279E9F0F55B7B2F2AFA9245B9A580D86
jbs=dx.window_handles #获取所有窗口句柄
print(jbs)
dx.switch_to.window(jbs[1]) #面向所有窗口的第一个窗口
sleep(2)
dx.find_element_by_link_text("我的订单").click()
(2)通过句柄切换窗口(三个窗口的排序0,2,1)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("https://www.jd.com/")'
dx.execute_script(w)
print(dx.title)
w1='window.open("https://www.hao.com/")'
dx.execute_script(w1)
jb=dx.current_window_handle #获取当前窗口句柄
print(jb) #CDwindow-279E9F0F55B7B2F2AFA9245B9A580D86
jbs=dx.window_handles #获取所有窗口句柄
print(jbs)
dx.switch_to.window(jbs[2])
sleep(2)
dx.find_element_by_link_text("我的订单").click()
(3)如果有多个窗口通过for 循环判断,确定窗口
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("https://www.jd.com/")'
dx.execute_script(w)
print(dx.title)
w1='window.open("https://www.hao.com/")'
dx.execute_script(w1)
jb=dx.current_window_handle #获取当前窗口句柄
print(jb) #CDwindow-279E9F0F55B7B2F2AFA9245B9A580D86
jbs=dx.window_handles #获取所有窗口句柄
for i in jbs:
dx.switch_to.window(i)
if "京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!" in dx.title:
break
print(dx.title)
sleep(2)
dx.find_element_by_link_text("我的订单").click()
三、下拉框
网址:
https://yz.chsi.com.cn/zsml/zyfx_search.jsp
from selenium import webdriver
from time import *
from selenium.webdriver.support.select import Select
dx=webdriver.Chrome()
dx.get("https://yz.chsi.com.cn/zsml/zyfx_search.jsp")
print(dx.title)
dx.maximize_window()
sleep(2)
wz=dx.find_element_by_id("ssdm") #定位
# Select(wz).select_by_index(2) #通过索引选择
# Select(wz).select_by_value("12") #通过value值选择
Select(wz).select_by_visible_text("(12)天津市") #通文本内容定位
四、弹框
一、alert 弹框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url=r"file:///D:/%E6%B5%8B%E8%AF%95%E8%AF%BE%E7%A8%8B%E8%AE%B0%E5%BD%95/HTML%E5%BC%B9%E6%A1%86/html%E5%BC%B9%E6%A1%86/alert%E5%BC%B9%E6%A1%86.html"
dx.get(url)
print(dx.title)
time(1)
tk=dx.switch_to.alert #切换到弹框中
tk.dismiss() #取消
二、确认型弹框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url=r"file:///E:/dcs/two/selenium/%E5%BC%B9%E6%A1%86/enter.html"
dx.get(url)
print(dx.title)
dx.find_element_by_class_name("alert").click()
tk=dx.switch_to.alert
tk.dismiss()
(三)输入型弹框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="file:///D:/%E6%B5%8B%E8%AF%95%E8%AF%BE%E7%A8%8B%E8%AE%B0%E5%BD%95/HTML%E5%BC%B9%E6%A1%86/html%E5%BC%B9%E6%A1%86/prompt.html"
dx.get(url)
print(dx.title)
dx.find_element_by_class_name("alert").click() #定位 点击
tk=dx.switch_to.alert #切换到弹框
tk.send_keys("ddd")
tk.accept() #确认
(四)上传文件类型弹框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="file:///E:/dcs/two/selenium/%E5%BC%B9%E6%A1%86/upload_file.html"
dx.get(url)
print(dx.title)
dx.find_element_by_id("file").send_keys(r"E:\az\f\ck\ddd.txt")#定位 发送到本地地址
(五)iframe框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url=r"https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&state=5DFCF10A905CCC9B086DD961CBBE1C550A9F09C40DA8C60F5B10F90423E4722D2956CD16E74FEF942C44EBB8ACC6F914&client_id=100273020&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fuuid%3Dbff66901b7ce422fb351b3b36ec54a4c"
dx.get(url)
print(dx.title)
k=dx.find_element_by_id("ptlogin_iframe") #定位frame框
dx.switch_to.frame(k) #进入frame框
sleep(2)
dx.find_element_by_link_text("密码登录").click()
sleep(2)
dx.switch_to.default_content() #退出frame框
dx.find_element_by_link_text("服务协议").click()
五、滚动条url="https://www.jd.com/" X轴Y轴
(1)
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="https://www.jd.com/"
dx.get(url)
j='window.scrollTo(0,2000)'
dx.execute_script(j)
sleep(3)
j1='window.scrollTo(0,0)' #滚动到原始点
dx.execute_script(j1)
(2)
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="https://www.jd.com/"
dx.get(url)
dx.maximize_window()
sleep(2)
js="var d=document.documentElement.scrollTop=2000"dx.execute_script(js)
sleep(3)
js1="var d=document.documentElement.scrollTop=0"
dx.execute_script(js1)
(3)
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="https://www.jd.com/"
dx.get(url)
dx.maximize_window()
sleep(2)
n=0
while n<5000:
n+=1000
js="var d=document.documentElement.scrollTop="+str(n)
dx.execute_script(js)
sleep(3)
六、快捷键: 网址:https://www.baidu.com/
(1) 输入、全选、剪切、粘贴、确认
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="https://www.baidu.com/"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("kw").send_keys("dcs")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.CONTROL,"a")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.CONTROL,"x")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.CONTROL,"v")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.ENTER)
(2)通过封装(简化代码)
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="https://www.baidu.com/"
dx.get(url)
dx.maximize_window()
def kj(lj,*a):
dx.find_element_by_id(lj).send_keys(*a)
sleep(3)
if __name__ == '__main__':
kj("kw","dc")
kj("kw", Keys.CONTROL,"a")
kj("kw", Keys.CONTROL, "x")
kj("kw", Keys.CONTROL, "v")
kj("kw", Keys.ENTER)
七、自动化用例
案例:http://cms.duoceshi.cn/manage/login.do
1.编写用例
(1)登录
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
(2)登录=用户中心=用户管理
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
sleep(2)
dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click() #定位菜单
sleep(2)
dx.find_element_by_link_text("用户管理").click()
(3)登录==系统管理==栏目设置
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
sleep(2)
dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
sleep(2)
dx.find_element_by_link_text("栏目设置").click()
八、封装
(1)每个用例都是独立(运行前调用登录)
from selenium import webdriver
from time import *
class Cms(object):
def __init__(self):
pass
def dl(self):
self.dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
self.dx.get(url)
self.dx.maximize_window()
self.dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
self.dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
self.dx.find_element_by_id("loginBtn").click()
sleep(3)
def yhgl(self):
c=self.dl()
self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click()
sleep(2)
self. dx.find_element_by_link_text("用户管理").click()
def lmcx(self):
c = self.dl()
self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
sleep(2)
self.dx.find_element_by_link_text("栏目设置").click()
if __name__ == '__main__':
d=Cms()
# d.dl()
# d.yhgl()
d.lmcx()
(2)每个用例执行前都会运行构造函数 登录
from selenium import webdriver
from time import *
class Cms(object):
def __init__(self):
self.dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
self.dx.get(url)
self.dx.maximize_window()
self.dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
self.dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
self.dx.find_element_by_id("loginBtn").click()
sleep(3)
def yhgl(self):
self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click()
sleep(2)
self. dx.find_element_by_link_text("用户管理").click()
def lmcx(self):
self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
sleep(2)
self.dx.find_element_by_link_text("栏目设置").click()
if __name__ == '__main__':
d=Cms()
# d.yhgl()
d.lmcx()
(3)
from selenium import webdriver
from time import *
class Cms(object):
def __init__(self):
self.dx = webdriver.Chrome()
url = "http://cms.duoceshi.cn/manage/login.do"
self.dx.get(url)
self.dx.maximize_window()
def dl(self):
self.dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
self.dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
self.dx.find_element_by_id("loginBtn").click()
sleep(3)
def yhgl(self):
c=self.dl()
self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click()
sleep(2)
self. dx.find_element_by_link_text("用户管理").click()
def lmcx(self):
c = self.dl()
self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
sleep(2)
self.dx.find_element_by_link_text("栏目设置").click()
if __name__ == '__main__':
d=Cms()
d.dl()
# d.yhgl()
d.lmcx()
版权归原作者 AgostoDu 所有, 如有侵权,请联系我们删除。