selenium常用的方法

1、基础用法
image
2、打开方式:
(1)get打开,打开两个页面(覆盖)
image
(2)打开多个链接,重开:window.open
image
image
二、时间等待
(1)sleep (秒数) 强制等待 (重点)
image

(2)implicitly__wait(秒数)隐性等待(重点)
当元素没有找到就轮询查找,找到立即执行
image

(3)werdriverwait  显性等待(了解即可) 等待某个元素,某个状态,满足,执行下一步

三、refresh刷新、back返回、 forword切换下一个
(1)refresh刷新
image
(2)back返回

image
(3) forword切换下一个

四、设置窗口的大小、窗口最大化

(1)窗口最大化dx.set_window_size(500,500)

image
(2)窗口最大化
image

五、close 关闭当前窗口、quit关闭所有窗口

(1)close 关闭当前窗口
image
(2)关闭所有的窗口
image

六、title 标题
image
image

七、send_key  输入
image
image

(八) click 点击
image

(九)切换页面
(1)查看浏览器打开后,当前窗口都在第一个浏览器中
有报错:
from  selenium  import  webdriver  #导入selenium 模块中webdriver函数
import time  #导入时间
dx=webdriver.Chrome() # 创建对象
time.sleep(2) #时间休眠
dx.get('https://www.baidu.com/') #get 打开网站
time.sleep(2) #休眠
print(dx.title)  #打印标题
bd="window.open('http://49.233.201.254:8080/cms/manage/login.do')" #打开cms
dx.execute_script(bd) #执行脚本
print(dx.title)  #打印抬头
dx.find_element_by_id("userAccount").send_keys("admin")  #输入账号
time.sleep(2) #时间休眠
dx.find_element_by_id("loginPwd").send_keys("123456")  #输入密码
time.sleep(2) #时间休眠
dx.find_element_by_id("loginBtn").click() #点击登录

(2)切换窗口
1.current_window_handle #获取当前的句柄2.dx.window_handles  获取所有的句柄3、switch_to.window(jbs[1]) 切换窗口  (句柄 【索引】)from  selenium  import  webdriver  #导入selenium 模块中webdriver函数
import time  #导入时间
dx=webdriver.Chrome() # 创建对象
time.sleep(2) #时间休眠
dx.get('https://www.baidu.com/') #get 打开网站
time.sleep(2) #休眠
print(dx.title)  #打印标题
bd="window.open('http://49.233.201.254:8080/cms/manage/login.do')" #打开cms
dx.execute_script(bd) #执行脚本
print(dx.title)  #打印抬头
dqjb=dx.current_window_handle #获取当前的句柄
jbs=dx.window_handles  #获取所有的句柄
dx.switch_to.window(jbs[1])  #切换 窗口  按照句柄的索引 位  0,1  三位数021
print(jbs)
dx.find_element_by_id("userAccount").send_keys("admin")  #输入账号
time.sleep(2) #时间休眠
dx.find_element_by_id("loginPwd").send_keys("123456")  #输入密码
time.sleep(2) #时间休眠
dx.find_element_by_id("loginBtn").click() #点击登录

(3)三个窗口(021)
from  selenium  import  webdriver  #导入selenium 模块中webdriver函数
import time  #导入时间
dx=webdriver.Chrome() # 创建对象
time.sleep(2) #时间休眠
dx.get('https://www.baidu.com/') #get 打开网站
time.sleep(2) #休眠
print(dx.title)  #打印标题
bd="window.open('http://49.233.201.254:8080/cms/manage/login.do')" #打开cms
dx.execute_script(bd) #执行脚本
print(dx.title)  #打印抬头
bd="window.open('https://www.jd.com/')" #打开cms
dx.execute_script(bd) #执行脚本
print(dx.title)  #打印抬头
dqjb=dx.current_window_handle #获取当前的句柄
jbs=dx.window_handles  #获取所有的句柄
print(jbs)
dx.switch_to.window(jbs[2])  #切换 窗口  按照句柄的索引 位  0,1  三位数021
print(jbs)
dx.find_element_by_id("userAccount").send_keys("admin")  #输入账号
time.sleep(2) #时间休眠
dx.find_element_by_id("loginPwd").send_keys("123456")  #输入密码
time.sleep(2) #时间休眠
dx.find_element_by_id("loginBtn").click() #点击登录按钮
time.sleep(2) #时间休眠

(4)通过for 判断是哪一个接口(title)
image
from  selenium  import  webdriver  #导入selenium 模块中webdriver函数
import time  #导入时间
dx=webdriver.Chrome() # 创建对象
time.sleep(2) #时间休眠
dx.get('https://www.baidu.com/') #get 打开网站
time.sleep(2) #休眠
print(dx.title)  #打印标题
bd="window.open('http://49.233.201.254:8080/cms/manage/login.do')" #打开cms
dx.execute_script(bd) #执行脚本
print(dx.title)  #打印抬头
bd="window.open('https://www.jd.com/')" #打开cms
dx.execute_script(bd) #执行脚本
print(dx.title)  #打印抬头
dqjb=dx.current_window_handle #获取当前的句柄
jbs=dx.window_handles  #获取所有的句柄
for i in jbs:  #遍历所有的句柄
dx.switch_to.window(i) #切换句柄
if  "过期更新" in  dx.title:
break
print(dx.title)  # 打印抬头
dx.find_element_by_id("userAccount").send_keys("admin")  #输入账号
time.sleep(2) #时间休眠
dx.find_element_by_id("loginPwd").send_keys("123456")  #输入密码
time.sleep(2) #时间休眠
dx.find_element_by_id("loginBtn").click() #点击登录按钮
time.sleep(2) #时间休眠

posted @ 2026-05-18 17:54  刘sir金牌讲师  阅读(12)  评论(0)    收藏  举报