selenium之实战

selnium 之实际案例

1、文本框和密码框定位、按钮

案例链接:http://49.233.201.254:8080/cms/manage/login.do

案例:

from selenium import webdriver

from time import *

dx=webdriver.Chrome()

dx.get("http://49.233.201.254:8080/cms/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() #登录按钮

注意:

send _keys 输入

click() 点击

2、链接,隐藏框

案例1:链接案例

网站:http://www.baidu.com

from selenium import webdriver

from time import *

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

dx.maximize_window()

sleep(2)

dx.find_element_by_link_text("hao123").click()

案例2:隐藏框 shift+ctrl+c查看隐藏元素

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

dx.maximize_window()

sleep(2)

wz=dx.find_element_by_link_text('更多')

ActionChains(dx).move_to_element(wz).perform()

sleep(2)

dx.find_element_by_xpath('//*@id="s-top-more"/div5/a/div').click()

案例3:selenium中的三种等待方式

(1)强制等待

设置固定的线程休眠时间

(2)隐式等待

设置固定的休眠时间前执行完成,就会执行下一步

(3)显式等待(理解)

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

dx.implicitly_wait(20) # 隐式等待

dx.maximize_window()

sleep(10) #强制等待

wz=dx.find_element_by_link_text('更多')

ActionChains(dx).move_to_element(wz).perform()

dx.implicitly_wait(20)

案例4:获取元素的文本(text)

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

wb=dx.find_element_by_id('s-top-loginbtn').text

print(wb)

if wb=="登 录":

print("0k")

else:

print("no")

案例5:获取窗口标题 title

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

print(dx.title) #百度一下,你就知道

dx.get("http://www.jd.com")

print(dx.title) #京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

print(dx.title) #百度一下,你就知道

dx.find_element_by_link_text("hao123").click()

print(dx.title) #百度一下,你就知道

案例6:断言

第一种if语句(无论对错,都有输出)

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

if dx.title=="百度一下,你就知道":

print("正确")

else:

print("错误")

第二种 assert (对的就执行下一步,错直接报错)

安立路

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

assert dx.title=="bai" #AssertionError

print(1)

案例7:设置窗口

(1)窗口最大化(maximize_window())

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

dx.maximize_window()

(2)设置窗口的大小(set_window_size)

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

dx.set_window_size(500,500) #设置窗口大小

(3)获取窗口大小

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

sleep(2)

print(dx.get_window_size()) #{'width': 1050, 'height': 850}

案例8:

refresh刷新

back返回上一步

forward() #返回下一步

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

sleep(3)

dx.refresh() #刷新

dx.get("http://www.taobao.com")

dx.back()#返回上一步

sleep(4)

dx.forward() #返回下一步

案例9:

close() 关闭当前浏览器

quit() 关闭所有的浏览器

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

sleep(2)

print(dx.title)

dx.find_element_by_link_text("hao123").click()

print(dx.title)

dx.close() #关闭百度的网页

dx.quit() #全部关闭

案例10:切换窗口

(1)、重开窗口(dx.execute_script('window.open("网站")'))

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

sleep(2)

print(dx.title)

dx.execute_script('window.open("http://www.jd.com")')

print(dx.title)

(2)切换窗口(2个窗口0,1)

a、current_window_handle #获取当前句柄

b、window_handles #获取所有句柄

c、切换窗口:switch_to.window【句柄【索引】】

案:两个窗口 0,1

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

sleep(2)

print(dx.title)

dx.execute_script('window.open("http://www.jd.com")')

print(dx.title)

jb=dx.current_window_handle #获取当前句柄

print(jb) #8AF0E28C861F3F35B32F9FEC5F1DC95D

handles=dx.window_handles #获取所有句柄

print(handles) # '8AF0E28C861F3F35B32F9FEC5F1DC95D', '69DBE3976128FE0052F4F9D1606328A4'

dx.switch_to.window(handles1) #q

print(dx.title)

sleep(2)

dx.find_element_by_xpath('//*@id="ttbar-login-2024"/a2').click() #免费注册

切换窗口:(3个窗口0,1,2)

案例:

from selenium import webdriver

from time import *

from selenium.webdriver.common.action_chains import ActionChains

dx=webdriver.Chrome()

dx.get("http://www.baidu.com")

sleep(2)

dx.execute_script('window.open("http://www.taobao.com")')

print(dx.title)

dx.execute_script('window.open("http://www.jd.com")')

print(dx.title)

jb=dx.current_window_handle #获取当前句柄

print(jb) #8AF0E28C861F3F35B32F9FEC5F1DC95D

handles=dx.window_handles #获取所有句柄

print(handles) #

dx.switch_to.window(handles1) #q

print(dx.title)

sleep(2)

dx.find_element_by_xpath('//*@id="ttbar-login-2024"/a2').click() #免费注册

切换窗口:(多个窗口通过if判断进行切换)

from selenium import webdriver #导入selemium 模块中的webdriver

dx=webdriver.Chrome() #创建一个webdriver的对象

dx.get("http://49.233.201.254:8080/cms/manage/login.do") #通过对象调用get打开网址

dx.implicitly_wait(2) #等待时间 2秒

ck="window.open('https://www.jd.com/')" #窗口打开网站

dx.execute_script(ck) #执行脚本,窗口

print(dx.title)

ck="window.open('https://www.baidu.com/')" #窗口打开网站

dx.execute_script(ck) #执行脚本,窗口

print(dx.title) #

dx.find_element_by_id("userAccount").send_keys("admin")

jbs=dx.window_handles #获取所有的句柄

print(jbs)

jb=dx.current_window_handle #获取当前的句柄

print(jb)

for i in jbs: #for 循环遍历句柄

dx.switch_to.window(i) #切换窗口

if "百度一下,你就知道" in dx.title:

break

jb=dx.current_window_handle #获取当前的句柄

print(jb)

dx.implicitly_wait(3)

dx.find_element_by_id('chat-textarea').send_keys("切换窗口")

案例11:下拉框

三种方法:

select_by_index(1) 通过索引定位

select_by_value()通过value定位

select_by_visible_text() 通过文本定位

案例:

from selenium import webdriver

from selenium.webdriver.support.select import Select

import time

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get("file:///E:/dcs/two/ui%E8%87%AA%E5%8A%A8%E5%8C%96/%E5%BC%B9%E6%A1%86/select.html") #打开百度网站

dx.maximize_window()

time.sleep(3)

wz=dx.find_element_by_id('dropdown')

Select(wz).select_by_index(1) #索引 索引从0开始

Select(wz).select_by_value('option3') #值

Select(wz).select_by_visible_text('绍兴') #文本内容

案例12:alert弹框

from selenium import webdriver

import time

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get("file:///E:/dcs/two/ui%E8%87%AA%E5%8A%A8%E5%8C%96/%E5%BC%B9%E6%A1%86/alert%E5%BC%B9%E6%A1%86.html") #打开百度网站

time.sleep(3)

tk=dx.switch_to.alert #切换弹框

tk.dismiss() #取消

time.sleep(2)

tk.accept() #确认

案例13:确认型弹框

from selenium import webdriver

import time

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get('file:///E:/dcs/two/ui%E8%87%AA%E5%8A%A8%E5%8C%96/%E5%BC%B9%E6%A1%86/enter.html')

time.sleep(3)

dx.find_element_by_class_name('alert').click()

tk=dx.switch_to.alert #切换弹框

time.sleep(2)

tk.accept() #确认

tk.dismiss() #取消

14、输入型弹框

from selenium import webdriver

import time

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get('file:///E:/dcs/two/ui%E8%87%AA%E5%8A%A8%E5%8C%96/%E5%BC%B9%E6%A1%86/prompt.html')

time.sleep(3)

dx.find_element_by_class_name('alert').click()

tk=dx.switch_to.alert #切换弹框

tk.send_keys("www.baidu.com") #输入网址

tk.accept() #确认

15、上传文件类型弹框

案例:

from selenium import webdriver

import time

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get("file:///E:/dcs/two/ui%E8%87%AA%E5%8A%A8%E5%8C%96/%E5%BC%B9%E6%A1%86/upload_file.html")

time.sleep(3)

dx.find_element_by_id('file').send_keys(r'E:\dcs\笔记\第一阶段\linux1.txt')

16、iframe框

切换框:witch_to.frame(wz)

退出框:switch_to.default_content()

案例:

from selenium import webdriver

import time

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get("https://graph.qq.com/oauth2.0/show?which=Login\&display=pc\&response_type=code\&state=21A144522CC9CCCA868302938D25937381C8CD155B1292720FA03EDC0C1EA6808C1DE4EBA02A78D15C654086304A2EE0\&client_id=100273020\&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fuuid%3Dde3fac3713a34709bc5c3cf99d57f1e6")

time.sleep(3)

wz=dx.find_element_by_id('ptlogin_iframe')

dx.switch_to.frame(wz) #进入框

time.sleep(3)

dx.find_element_by_link_text("密码登录").click() #框内

dx.switch_to.default_content()# 退出框

time.sleep(3)

dx.find_element_by_link_text("服务协议").click() #框内

17、滚动条定位

滚动条是由js代码编写的

方法:execute_script()

案例:

from selenium import webdriver

import time

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get('https://www.jd.com/')

time.sleep(3)

js='window.scrollTo(0,2000)'向下滑动

dx.execute_script(js)

time.sleep(3)

js1='window.scrollTo(0,0)' #向上滑

dx.execute_script(js1)

18、 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

import time

from selenium.webdriver.common.keys import Keys

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get('https://www.baidu.com/')

time.sleep(3)

dx.find_element_by_id('chat-textarea').send_keys('dcs') #输入内容

time.sleep(3)

dx.find_element_by_id('chat-textarea').send_keys(Keys.CONTROL,'a') #全选内容

time.sleep(3)

dx.find_element_by_id('chat-textarea').send_keys(Keys.CONTROL,'x') #剪切内容

time.sleep(3)

dx.find_element_by_id('chat-textarea').send_keys(Keys.CONTROL,'v')#黏贴内容

time.sleep(3)

dx.find_element_by_id('chat-textarea').send_keys(Keys.ENTER) #确认内容

封装:

from selenium import webdriver

import time

from selenium.webdriver.common.keys import Keys

dx=webdriver.Chrome() #创建webdriver,设置浏览器

dx.get('https://www.baidu.com/')

time.sleep(3)

def j(p,*a):

dx.find_element_by_id(p).send_keys(*a)

time.sleep(2)

if name == 'main':

j('chat-textarea','dcs')

j('chat-textarea',Keys.CONTROL,'a')

j('chat-textarea', Keys.CONTROL, 'x')

j('chat-textarea', Keys.CONTROL, 'v')

j('chat-textarea', Keys.ENTER)

相关推荐
yumgpkpm1 小时前
(CDH 7)CDP Private Cloud Base 7.3.1 → Acceldata ODP 3.3.6.4 引擎迁移风险评估表
服务器·人工智能·hadoop·python·华为·zookeeper·hbase
Lost_the_wind1 小时前
MySQL 学习笔记三
笔记·学习·mysql
刘天远1 小时前
Agent成本核算实现:事件表、状态分布与Python归集
前端·数据库·人工智能·python
挨踢诗人1 小时前
玄讯CRM集成金蝶云星空解决方案
自动化
Python图像识别1 小时前
40-【2027毕设】YOLO11面部口罩检测识别系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·qt·课程设计
赵大仁1 小时前
开源清单怎么维护才不烂掉?GitHub Actions 每周查死链 + 软 404 检测实录
python·ci/cd·自动化·开源项目·踩坑·github actions·json schema
玩AI的奶茶1 小时前
24GB 显存能跑多大的模型?参数量、精度与显存占用对照表
人工智能·python·算法·ai·aigc·gpu算力·算力租赁
kimnoic1 小时前
Python操作Git命令的详细指南
开发语言·python
Python图像识别1 小时前
39-【2027毕设】YOLO11行人摔倒检测系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·qt·课程设计