十二、警告框处理

JavaScript中,关于消息提示框的方法有三个:

  • alert(message)方法用于显示带有一条指定消息和一个 OK 按钮的警告框。
  • confirm(message)方法用于显示一个带有指定消息和 OK 及取消按钮的对话框。如果用户点击确定按钮,则 confirm() 返回 true。如果点击取消按钮,则 confirm() 返回 false。
  • prompt(text,defaultText)方法用于显示可提示用户进行输入的对话框。如果用户单击提示框的取消按钮,则返回 null。如果用户单击确认按钮,则返回输入字段当前显示的文本。

来看个实现警告框示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>alert</title>
</head>
<body>

<input type="button" id="alertButton" value="alert" onclick="alertButton()">
<input type="button" id="confirmButton" value="confirm" onclick="confirmButton()">
<input type="button" id="promptButton" value="prompt" onclick="promptButton()">
<script>

    function alertButton() {
        alert('我是普通的alert提示框');
    };

    function confirmButton() {
        var msg = confirm('点击[确定]或者[取消]按钮');
        if (msg) {
            alert('你点击的是[确定按钮]');
        } else {
            alert('你点击的是[取消按钮]');
        }
    };

    function promptButton() {
        var msg = prompt('输入一个值:', '我是默认值');
        if (msg) {
            alert('输入的值为:\n' + msg);
        } else {
            alert('输入值为空');
        }
    };
</script>
</body>
</html>

selenium操作上面三种提示框有以下几种方法:

  • alertObject.text:获取提示的文本值。
  • alertObject.accept():点击『确认』按钮。
  • alertObject.dismiss():点击『取消』或者叉掉对话框。
  • alertObject.send_keys(message):输入文本,仅适用于prompt方法,因为alert和confirm的提示框没有输入框!!!并且,如果在非prompt类型的提示框使用alertObject.send_keys(message),会报错!

由于这些警告框的特性,我们F12都无法选中。所以,selenium在处理起来,首先要经过一个switch_to的过程。另外,当你看到提示框只有提示信息和一个确定按钮时,它就是alert提示框;当你看到提示框中有提示信息和确定/取消按钮都在时,它就是confirm提示框;当你看到提示信息和input框,并且确定和取消按钮都在时,它就是prompt提示框。为什么不提提示框中右上角的叉掉图标,这是根据浏览器的类型不同而决定,有的有这个图标,有的没有,比如Chrome和Firefox就没有,而IE就有。

selenium处理alert提示框

selenium在处理alert时,要经过:

  1. switch_to.alert方法将webdriver作用域切换到alert提示框上。
  2. 现在,我们可以使用text获取提示文本信息、accept()点击确认按钮、dismiss()点击取消或者叉掉提示框。
from selenium import webdriver
import os
import time

driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('filetest.html')
driver.get(file_path)

time.sleep(2)
driver.find_element_by_id('alertButton').click()
time.sleep(1)

#指定要操作alter
alertObject = driver.switch_to.alert  # 这里,alert方法不加括号,以为该方法被 @property 伪装成属性了,具体参考源码
#输出提示框文本信息
print(alertObject.text)  # text方法也被 @property 伪装成属性了
alertObject.accept()  # 点击确定按钮

selenium处理confirm提示框

selenium在处理confirm时,与处理alert类似:

  1. switch_to.alert方法将webdriver作用域切换到alert提示框上。
  2. 可以使用text获取提示文本信息、accept()点击确认按钮、dismiss()点击取消。
from selenium import webdriver
import os
import time

driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('filetest.html')
driver.get(file_path)

driver.find_element_by_id('confirmButton').click()
time.sleep(1)
alertObject = driver.switch_to.alert
print(alertObject.text)  # 打印提示信息
time.sleep(1)
alertObject.accept()  # 点击确定按钮
time.sleep(1)
alertObject.accept()  # 根据前端js代码逻辑,当点击确定按钮后会再弹出一个提示框,我们 点击确定

confirm提示框中也不能使用alertObject.send_keys()方法。

selenium处理prompt提示框

selenium在处理prompt时,与处理alert/confirm一样:

  1. switch_to.alert方法将webdriver作用域切换到alert提示框上。
  2. 现在,我们可以使用text获取提示文本信息、accept()点击确认按钮、dismiss()点击取消或者叉掉提示框。
  3. 现在可以使用alertObject.send_keys(message)方法了。
from selenium import webdriver
import os
import time

driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('filetest.html')
driver.get(file_path)

driver.find_element_by_id('promptButton').click()
time.sleep(1)
alertObject = driver.switch_to.alert
print(alertObject.text)  # 打印提示信息
time.sleep(1)

alertObject.send_keys('明天是个好日子')
print(alertObject.text)  # 打印提示信息
alertObject.accept()  # 点击确定按钮

print(alertObject.text)  # 打印提示信息
time.sleep(1)
alertObject.accept()  # 根据前端js代码,当点击确定按钮后会再弹出一个提示框,我们再次点击确定
time.sleep(2)

driver.find_element_by_id('promptButton').click()
time.sleep(1)
alertObject = driver.switch_to.alert
alertObject.send_keys('hello')
time.sleep(1)
alertObject.dismiss()  # 什么都不管,直接点击取消,会在弹出一个提示框
time.sleep(1)
# 现在弹出的是一个普通的提示框,点击确定和取消都无所谓,具体根据业务场景来决定
# alertObject.accept()
alertObject.dismiss()

这个地方需要注意,send_keys直接输入内容不会显示,需要使用accept()确认之后才会看到结果

posted @ 2019-10-24 14:43  酒剑仙*  阅读(531)  评论(1编辑  收藏  举报