Selenium定位浏览器弹窗方法实例总结

作者:慕城南风 时间:2022-07-03 05:17:24 

前言

在Selenium自动化测试过程中会遇到定位浏览器弹窗的情况,根据弹窗实现原理不同大致可分为以下几种定位方式。

1. alert、confirm、prompt类型弹框

这三种弹框是JavaScript核心对象window对象中的三种方法。

1.1 alert弹框

警告消息框 alert 方法有一个参数,即希望对用户显示的文本字符串。该字符串不是 HTML 格式。该消息框提供了一个“确定”按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说,用户必须先关闭该消息框然后才能继续进行操作。

Selenium定位浏览器弹窗方法实例总结

selenium处理alert() 提示框:

  • driver.switchTo().alert(); 获取alert

  • alert.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弹框

确认消息框 使用确认消息框可向用户问一个“是-或-否”问题,并且用户可以选择单击“确定”按钮或者单击“取消”按钮。confirm 方法的返回值为 true 或 false。该消息框也是模式对话框:用户必须在响应该对话框(单击一个按钮)将其关闭后,才能进行下一步操作。

Selenium定位浏览器弹窗方法实例总结

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弹框

提示消息框 提供了一个文本字段,用户可以在此字段输入一个答案来响应您的提示。该消息框有一个&ldquo;确定&rdquo;按钮和一个&ldquo;取消&rdquo;按钮。如果您提供了一个辅助字符串参数,则提示消息框将在文本字段显示该辅助字符串作为默认响应。否则,默认文本为 "<undefined>"。

Selenium定位浏览器弹窗方法实例总结

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弹窗是浏览器中比较好定位的弹窗,定位的方法与普通的元素一样。不过这里会有一个坑,明明可以找到这个按钮,但是就是定位不到。这个就是因为当前有div弹窗弹出的时候,需要设置一下等待时间,等页面元素加载完毕,再去做其他操作。这里用百度登陆为例子:

Selenium定位浏览器弹窗方法实例总结

3. 新标签页弹窗

新标签页弹窗,则需要进行窗口的切换。

弹出内容是嵌入的窗口解决思路:

# 打印所有的handle

all_handles = driver.window_handles

    print(all_handles)

# 切换到新的handle上

driver.switch_to.window(all_handles[N])

其中,获取的句柄下标从0开始,即第一个窗口为[0]、第二个窗口为[1],如此类推。使用switch_to.window方法切换到新标签页后就可以做其他操作了。

此处第一个窗口打开百度首页,在打开一个新窗口打开京东首页,在两个窗口之间进行切换。切换到不同的窗口之后,就可以用常规的方法进行元素的定位。

Selenium定位浏览器弹窗方法实例总结

4. 弹出框是iframe

driver.switch_to.frame("frame1")之后进行定位元素

具体定位方式查看我的另一篇博客:Selenium之定位及切换frame(iframe)

参考文章:

1.关于Python+selenium 定位浏览器弹窗元素

2.selenium定位弹框元素

3.https://www.jb51.net/article/156978.htm

4.selenium多个浏览器窗口

来源:https://blog.csdn.net/lovedingd/article/details/111089391

标签:selenium,定位,弹窗
0
投稿

猜你喜欢

  • CSS实例讲解:地图提示

    2007-05-11 17:04:00
  • python合并多个excel的详细过程

    2023-10-03 14:39:26
  • 分享个asp文件缓存代码,使程序从缓存读数据

    2011-03-09 19:47:00
  • python-for x in range的用法(注意要点、细节)

    2022-11-12 22:40:49
  • sqlserver 脚本和批处理指令小结

    2012-05-22 18:56:55
  • Python ZipFile模块详解

    2021-09-17 06:30:24
  • vitrualBox+ubuntu16.04安装python3.6最新教程及详细步骤

    2021-06-26 05:34:38
  • asp如何使用SMTP Service发送邮件?

    2010-06-05 12:43:00
  • Python叠加矩形框图层2种方法及效果

    2022-10-09 15:59:53
  • Python应用实现处理excel数据过程解析

    2022-10-24 11:52:17
  • Python中断言Assertion的一些改进方案

    2023-03-05 15:07:57
  • Python正规则表达式学习指南

    2021-04-11 15:21:16
  • 通过实例简单了解Python sys.argv[]使用方法

    2022-09-12 14:14:35
  • Python直接赋值与浅拷贝和深拷贝实例讲解使用

    2021-06-16 08:21:21
  • 分享3个非常实用的 Python 模块

    2023-08-07 18:21:33
  • Python 文件操作方法总结

    2023-11-29 11:36:28
  • python右对齐的实例方法

    2022-01-15 20:59:52
  • PHP读取txt文本文件并分页显示的方法

    2023-09-06 21:13:25
  • 解决python父线程关闭后子线程不关闭问题

    2023-11-28 22:01:56
  • Google的设计导引

    2008-04-06 14:18:00
  • asp之家 网络编程 m.aspxhome.com