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()
原文地址:https://www.cnblogs.com/zouzou-busy/p/11160503.html