Selenium alert 弹窗处理的示例代码

所属分类: 脚本专栏 / python 阅读数: 878
收藏 0 赞 0 分享

selenium提供switch_to_alert方法:捕获弹出对话框(可以定位alert、confirm、prompt对话框)

switch_to_alert()    --定位弹出对话框

text()               --获取对话框文本值

accept()             --相当于点击“确认”

dismiss()            --相当于点击“取消”

send_keys()          --输入值(alert和confirm没有输入对话框,所以就不用能用了,只能使用在prompt里)

页面弹窗有 3 种类型:

  • alert(警告信息)
  • confirm(确认信息)
  • prompt(提示输入)

对于页面出现的 alert 弹窗,Selenium 提供如下方法:

序号 方法/属性 描述
1 accept() 接受
2 dismiss() 取消
3 text 获取显示的文本
4 send_keys() 输入内容

对应处理方式:

  1. alert(警告信息):WebDriver.switch_to.alert.accept()
  2. confirm(确认信息):
    1. WebDriver.switch_to.alert.accept()
    2. WebDriver.switch_to.alert.dismiss()
  3. prompt(提示输入):WebDriver.switch_to.alert.send_keys()

alert(警告信息)弹出框

alert(警告信息)弹出框,目的是提示通知信息,只需要用户看完点击确认即可。

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome() # 打开浏览器
driver.get("http://sahitest.com/demo/alertTest.htm") # 跳转至测试页面
sleep(1)
element = driver.find_element_by_name("b1") # 定位
element.click() # 点击
sleep(1)
alert = driver.switch_to.alert # 切换到弹窗
print(alert.text) # 打印弹窗显示的信息:Alert Message
alert.accept() # 接受
sleep(2)

driver.quit() # 关闭浏览器

confirm(确认信息)弹出框

confirm(确认信息)弹出框,主要是让用户来确定是否要执行某个操作。比如,淘宝,京东等,删除订单是弹出此类弹出,让用户确定是否删除,避免用户误操作。

confirm(确认信息)弹出框提供两种选择,“确认” 或者 “取消”。只需要选择其中一个即可:

  • 确认:WebDriver.switch_to.alert.accept()
  • 取消:WebDriver.switch_to.alert.dismiss()
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome() # 打开浏览器
driver.get("http://sahitest.com/demo/confirmTest.htm") # 跳转至测试页面
sleep(1)
element = driver.find_element_by_name("b1") # 定位
# 1、接受
element.click() # 点击
sleep(1)
alert = driver.switch_to.alert # 切换到弹窗
print(alert.text) # 打印弹窗显示的信息:Alert Message
alert.accept() # 接受
sleep(2)

# 2、取消
element.click() # 点击
sleep(1)
alert = driver.switch_to.alert # 切换到弹窗
print(alert.text) # 打印弹窗显示的信息:
alert.dismiss()
sleep(2)

driver.quit() # 关闭浏览器

prompt(提示输入)弹出框

prompt(提示输入)弹出框,目的是需要用户先输入信息,再做提交。Selenium 提供输入信息的方法是:

WebDriver.switch_to.alert.send_keys()
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome() # 打开浏览器
driver.get("http://sahitest.com/demo/promptTest.htm") # 跳转至测试页面
sleep(1)
element = driver.find_element_by_name("b1") # 定位
# 1、接受
element.click() # 点击
sleep(1)
alert = driver.switch_to.alert # 切换到弹窗
print(alert.text) # 打印弹窗显示的信息:Alert Message
alert.send_keys("自动化测试") # 输入内容
sleep(1)
alert.accept() # 接受
sleep(2)

driver.quit() # 关闭浏览器

注意 : 有些弹窗并非浏览器的 alert 窗口,而是 html元素,对于这种对话框,只需要通过之前介绍的选择器选中,并进行相应的操作即可。

更多精彩内容其他人还在看

使用keras实现非线性回归(两种加激活函数的方式)

这篇文章主要介绍了使用keras实现非线性回归(两种加激活函数的方式),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python线性插值解析

这篇文章主要介绍了python线性插值解析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python中flatten( ),matrix.A用法说明

这篇文章主要介绍了Python中flatten( ),matrix.A用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python cv2.resize函数high和width注意事项说明

这篇文章主要介绍了python cv2.resize函数high和width注意事项说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python 图像插值 最近邻、双线性、双三次实例

这篇文章主要介绍了python 图像插值 最近邻、双线性、双三次实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python-openCV开运算实例

这篇文章主要介绍了Python-openCV开运算实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python使用opencv resize图像不进行插值的操作

这篇文章主要介绍了python使用opencv resize图像不进行插值的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Python-OpenCV消除图像中孤立的小区域操作

这篇文章主要介绍了使用Python-OpenCV消除图像中孤立的小区域操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python右对齐的实例方法

在本篇文章里小编给大家整理的是关于python右对齐的实例方法,有需要的朋友们可以学习参考下。
收藏 0 赞 0 分享

python主要用于哪些方向

在本篇文章里小编给大家整理了一篇关于python用于的方向的相关文章,有需要的阅读下吧。
收藏 0 赞 0 分享
查看更多