selenium--操作JS弹框
前戏
我们常见的弹框有三种,一种是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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)