python3.6+selenium操作JavaScript弹窗

self.driver.switch_to_alert()做弹窗处理的时候,杯摒弃掉了,但是不影响使用,但是看见报错依旧不舒服,于是寻找一些方法终于解决了这个问题,希望有所帮助
被测网页如下:

在这里插入图片描述


代码如下:
import unittest
from selenium.common.exceptions import NoAlertPresentException
import time
from selenium import webdriver

class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executable_path=‘D:\python\driver\geckodriver’)
def test_HandleFrame(self):
url=“file:///D:/alert.html”
#启动自定义网页
self.driver.get(url)
#找到id=button的元素
button=self.driver.find_element_by_id(“button”)
button.click()
try:
#使用driver.switch_to_alert()方法获取alert对象
#alert=self.driver.switch_to_alert()
alert=self.driver.switch_to.alert
time.sleep(2)
#断言文字内容是否是“这是一个alert弹出框”
self.assertEqual(alert.text,u"这是一个alert弹出框")
#调用alert对象的accept()方法,模拟鼠标单击alert弹窗上的确定按钮
alert.accept()
print(“这是一个alert弹出框”)
except NoAlertPresentException as e:
self.fail(“尝试操作alert框未被找到”)
print(e)

if name == ‘main’:
unittest.main()

相关文章

转载地址:https://www.cnblogs.com/mini-monkey/p/12104821...
web自动化测试过程中页面截图相对比较简单,可以直接使用sel...
目录前言一、Selenium简介二、浏览器驱动1.浏览器驱动参考2....
一、iframe的含义:iframe是HTML中框架的一种形式,在对界面...
转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.to...
'''##**认识selenium**​**下载:pipinstall...