Selenium定位浏览器弹窗方法实例总结
2022-06-22 12:10:36 来源:易采站长站 作者:
目录
前言1. alert、confirm、prompt类型弹框1.1 alert弹框1.2 confirm弹框1.3 prompt弹框2. div弹框3. 新标签页弹窗4. 弹出框是iframe总结前言
在Selenium自动化测试过程中会遇到定位浏览器弹窗的情况,根据弹窗实现原理不同大致可分为以下几种定位方式。
1. alert、confirm、prompt类型弹框
这三种弹框是JavaScript核心对象window对象中的三种方法。
1.1>
警告消息框 alert 方法有一个参数,即希望对用户显示的文本字符串。该字符串不是 HTML 格式。该消息框提供了一个“确定”按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说,用户必须先关闭该消息框然后才能继续进行操作。

selenium处理alert() 提示框:
- driver.switchTo().alert(); 获取alertalert.accept(); 点确定alert.dismiss(); 点取消alert.getText();获取alert的内容
alert弹框定位代码:
try{
Alert alert =driver.switchTo().alert(); //使用driver.switchTo().alert()方法获取到alert对象
Assert.assertEquals("弹框实际文本", alert.getText()); //断言弹框文本是否和预期一致
alert.accept(); //点击确定
// alert.dismiss(); //点击取消
}catch(NoAlertPresentException exception){ //弹框未显示,则跑出异常
Assert.fail("尝试操作的alert框没有被找到");
exception.printStackTrace();
}
或官网推荐方法
# Click the link to activate the alert driver.find_element(By.LINK_TEXT, "See an example alert").click() # Wait for the alert to be displayed and store it in a variable alert = wait.until(expected_conditions.alert_is_present()) # Store the alert text in a variable text = alert.text # Press the OK button alert.accept()
1.2 confirm弹框
确认消息框>

selenium处理confirm() 提示框:
同alert一致
try{
Alert alert =driver.switchTo().alert();
Assert.assertEquals("弹框实际文本", alert.getText());
alert.accept();
// alert.dismiss();
}catch(NoAlertPresentException exception){
Assert.fail("尝试操作的alert框没有被找到");
exception.printStackTrace();
}
或官网推荐方法
# Click the link to activate the alert driver.find_element(By.LINK_TEXT, "See a sample confirm").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = driver.switch_to.alert # Store the alert text in a variable text = alert.text # Press the Cancel button alert.dismiss()
1.3 prompt弹框
提示消息框>

selenium处理prompt() 提示框:
try{
Alert alert =driver.switchTo().alert();
Assert.assertEquals("弹框实际文本", alert.getText());
alert.sendKeys("promt框中输入的内容");
alert.accept();
// alert.dismiss();
}catch(NoAlertPresentException exception){
Assert.fail("尝试操作的alert框没有被找到");
exception.printStackTrace();
}
或官网推荐方法
# Click the link to activate the alert
driver.find_element(By.LINK_TEXT, "See a sample prompt").click()
# Wait for the alert to be displayed
wait.until(expected_conditions.alert_is_present())
# Store the alert in a variable for reuse
alert = Alert(driver)
# Type your message
alert.send_keys("Selenium")
# Press the OK button
alert.accept()
2.>
div弹窗是浏览器中比较好定位的弹窗,定位的方法与普通的元素一样。不过这里会有一个坑,明明可以找到这个按钮,但是就是定位不到。这个就是因为当前有div弹窗弹出的时候,需要设置一下等待时间,等页面元素加载完毕,再去做其他操作。这里用百度登陆为例子:

3.>
新标签页弹窗,则需要进行窗口的切换。
弹出内容是嵌入的窗口解决思路:
# 打印所有的handle
all_handles = driver.window_handles
print(all_handles)
# 切换到新的handle上
driver.switch_to.window(all_handles[N])
其中,获取的句柄下标从0开始,即第一个窗口为[0]、第二个窗口为[1],如此类推。使用switch_to.window方法切换到新标签页后就可以做其他操作了。
此处第一个窗口打开百度首页,在打开一个新窗口打开京东首页,在两个窗口之间进行切换。切换到不同的窗口之后,就可以用常规的方法进行元素的定位。

4.>
driver.switch_to.frame("frame1")之后进行定位元素
具体定位方式查看我的另一篇博客:Selenium之定位及切换frame(iframe)
参考文章:
1.关于Python+selenium 定位浏览器弹窗元素
2.selenium定位弹框元素
3.https://www.jb51.net/article/156978.htm
4.selenium多个浏览器窗口
总结
到此这篇关于Selenium定位浏览器弹窗方法的文章就介绍到这了,更多相关Selenium定位浏览器弹窗内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!













闽公网安备 35020302000061号