前戏

我们常见的弹框有三种,一种是alert弹框,一种是prompt弹框,还有一种是confirm弹框那他们有什么不同呢?不同点就是他们长的不一样,alert弹框有一段文字和一个确定按钮,如下

在来看一下prompt长什么样

confirm长这样

看完上面的三个框,大家应该能区分出什么框是哪种类型的了吧。。。

处理alert弹框

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" id="button"
onclick="alert('这是一个alert弹出框')" value="单击此按钮"> </body>
</html>
from selenium import webdriver
import time,unittest
from selenium.common.exceptions import NoAlertPresentException class Test_Alert(unittest.TestCase):
def test_HandleAlert(self):
url = r'E:\JSSCRIPT.html'
self.driver = webdriver.Chrome()
self.driver.get(url)
button = self.driver.find_element_by_id('button')
button.click()
try:
# 使用driver.switch_to.alert()方法获取alert对象
alert = self.driver.switch_to_alert()
time.sleep(2)
# 断言弹出框里的内容
self.assertEqual(alert.text, '这是一个alert弹出框')
# 调用alert对象的accept()方法,模拟鼠标单击alert弹窗上的“确定”按钮
alert.accept()
except NoAlertPresentException as e:
print(e) test1 = Test_Alert()
test1.test_HandleAlert()

处理prompt弹框

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" id="button"
onclick="prompt('这是一个prompt弹出框')" value="单击此按钮"> </body>
</html>
from selenium import webdriver
import time,unittest
from selenium.common.exceptions import NoAlertPresentException class Test_prompt(unittest.TestCase):
def test_HandleAlert(self):
url = r'E:\JSSCRIPT.html'
self.driver = webdriver.Chrome()
self.driver.get(url)
button = self.driver.find_element_by_id('button')
button.click()
try:
# 使用driver.switch_to.alert()方法获取alert对象
alert = self.driver.switch_to_alert()
time.sleep(2)
# 断言弹出框里的内容
self.assertEqual(alert.text, '这是一个prompt弹出框')
# 往框里输入值
alert.send_keys('我要搞自动化。。。') # 没输入但是也没报错
time.sleep(4)
alert.accept() # 模拟点击确定按钮
except NoAlertPresentException as e:
print(e) test1 = Test_prompt()
test1.test_HandleAlert()

处理confirm弹框

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" id="button"
onclick="prompt('这是一个confirm弹出框')" value="单击此按钮"> </body>
</html>
from selenium import webdriver
import time,unittest
from selenium.common.exceptions import NoAlertPresentException class Test_confirm(unittest.TestCase):
def test_HandleAlert(self):
url = r'E:\JSSCRIPT.html'
self.driver = webdriver.Chrome()
self.driver.get(url)
button = self.driver.find_element_by_id('button')
button.click()
try:
# 使用driver.switch_to.alert()方法获取alert对象
alert = self.driver.switch_to.alert
time.sleep(2)
# 断言弹出框里的内容
self.assertEqual(alert.text, '这是一个confirm弹出框')
# 往框里输入值
alert.send_keys('我要搞自动化。。。') # 没输入但是也没报错
time.sleep(4)
alert.accept() # 模拟点击确定按钮
alert.dismiss() # 点击取消按钮 和上面的取其一
except NoAlertPresentException as e:
print(e) test1 = Test_confirm()
test1.test_HandleAlert()

selenium--操作JS弹框的更多相关文章

  1. js弹框处理

    # -*- coding:utf-8 -*- """ js弹框处理 """ from selenium import webdriver d ...

  2. js弹框3秒后自动消失

    开发中有时候会需要最出弹框后,过几秒自动消失的效果,下面给大家分享一下我自己做的一个小案例. 案例中的弹框使用的是bootstrap里面的模态框,实现自动消失则用的是js中的setInterval方法 ...

  3. selenium对Alert弹框的多种处理

    Alert弹框是一个很烦人的控件,因为当前页面如果弹出了该弹框,你必须要处理它,不然你就不能操作页面的其它元素,下面我列出了alert弹框在多种场景下的处理办法. 明确知道系统哪个地方会弹alert ...

  4. 【前端开发】--js弹框

    js三种弹框 一.普通弹框 这类弹框就是仅仅是个提示作用,并不会做其它操作 关键词:alert()    这个没啥好说的,就是一个弹框.  二.判断弹框     这类框有一个判断作用 关键字:conf ...

  5. Java+Selenium操作日期时间选择框插件

    在自动化测试的时候我们经常会碰到下面的时间日期插件(这个时候这个文本框是不运行我们输入时间的), 我们可以用java获取当前日期,然后用Selenium结合JS代码就可以直接往文本框输入内容. 像这种 ...

  6. js弹框的3种方法

    js的三种弹框的方法 1.第一种 :  alert("1"); 2.第二种 :  window.open("Tests2.html"); var r = con ...

  7. 可以替代alert 的漂亮的Js弹框

    1 基本弹框 2确认框 3又一种确认框 4带返回的弹框 5带返回的探矿 6 6 一切尽在 http://t4t5.github.io/sweetalert/

  8. selenium 操作复选框

    场景 从上一节的例子中可以看出,webdriver可以很方便的使用findElement方法来定位某个特定的对象,不过有时候我们却需要定位一组对象, 这时候就需要使用findElements方法. 定 ...

  9. js弹框怎么获得父页面的元素

    js获取父页面的元素可以用$(window.parent.document).find("#customer_id").val();这里的customer_id表示父页面某一个元素 ...

随机推荐

  1. PostMan测试REST接口时候,如何绕过登录的验证

    原文地址:https://blog.csdn.net/qq_34178998/article/details/80361315 之前测试的时候,需要页面进行登录之后,才能让访问后台程序,但是在进行接口 ...

  2. Balking模式

    Balking模式讲的是如果现在不合适执行这个操作,或者没必要执行这个操作,就停止处理,直接返回 自动保存功能的实现逻辑一般都是隔一定时间自动执行存盘操作,存盘操作的前提是文件做过修改,如果文件没有执 ...

  3. SQL索引管理器 - 用于SQL Server和Azure上的索引维护的免费GUI工具

    我作为SQL Server DBA工作了8年多,管理和优化服务器的性能.在我的空闲时间,我想为宇宙和我的同事做一些有用的事情.这就是我们最终为SQL Server和Azure 提供免费索引维护工具的方 ...

  4. C#7语法快速参考-第一章 Hello World

    选择IDE 要开始使用C#编程,您需要一个支持微软.NET框架的集成开发环境(IDE).最受欢迎的选择是微软自己的Visual Studio.初学可以使用Visual Studio Community ...

  5. Postgresql中无则插入的使用方法INSERT INTO WHERE NOT EXISTS

    一.问题 Postgresql中无则插入的使用方法INSERT INTO WHERE NOT EXISTS,用法请参考样例. 二.解决方案 (1)PostgresSQL INSERT INTO tes ...

  6. MySQL中的存储过程、游标和存储函数

    MySQL中的存储过程首先来看两个问题: 1.什么是存储过程? 存储过程(Stored Procedure)是在数据库系统中,一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存 ...

  7. Django---CSRF的装饰器,CSRF的流程,JSON数据格式,ajax技术(基于JQ实现)

    Django---CSRF的装饰器,CSRF的流程,JSON数据格式,ajax技术(基于JQ实现) 一丶CSRF相关的装饰器 from django.utils.decorators import m ...

  8. SpringApplication到底run了什么(下)

    在上篇文章中SpringApplication到底run了什么(上)中,我们分析了下面这个run方法的前半部分,本篇文章继续开工 public ConfigurableApplicationConte ...

  9. springboot启动报错,Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

    报错: Error starting ApplicationContext. To display the conditions report re-run your application with ...

  10. Centos7添加磁盘并分区格式化

    1.安装前准备 [root@localhost ~]# yum install xfsprogs [root@localhost ~]# modprobe xfs [root@localhost ~] ...