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()