Selenium processing alert / confirm / prompt prompt box

 

About

back to the top

New understanding alert
First of all, not all of the alert can be called alert box.
JavaScript, concerning the process message box has three (although related alert about the same):

  • alert (message) block having a specified Warning message and an OK button for displaying.
  • confirm (message) to specify a method for displaying a message and an OK button and a Cancel dialog with. If the user clicks the OK button, then confirm () returns true. If you click the Cancel button, then confirm () returns false.
  • prompt (text, defaultText) method may prompt the user to display a dialog box for input. If the user clicks the Cancel button prompt box, null is returned. If the user clicks the OK button, then returns to the input text field currently displayed.

Look at an example:

<!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>

How to deal with selenium?
There are several ways the above three balloons selenium operation:

  • alertObject.text: Get tips text value.
  • alertObject.accept (): Click the "OK" button.
  • alertObject.dismiss (): Click "Cancel" or fork out the dialog box.
  • alertObject.send_keys (message): Enter the text, apply only to prompt method, because the alert and confirm the circles do not enter the box! ! ! Also, if you use alertObject.send_keys (message) in a non-prompt type message box will complain!

whatever,因为这些消息提示框的特性,我们『检查 or 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()点击取消或者叉掉提示框。
import time
from selenium import webdriver

driver = webdriver.Chrome()
try:

    driver.get(url='file:///M:/tests.html')
    time.sleep(2)
    driver.find_element_by_id('alertButton').click()
    time.sleep(1)
    # alertObject = driver.switch_to_alert()   # 不要使用这个方法了,因为该方法即将被弃用,当然,暂时还是可以使用,或者selenium版本较低也可以使用。
    alertObject = driver.switch_to.alert  # 这里,alert方法不加括号,以为该方法被 @property 伪装成属性了,具体参考源码
    print(alertObject.text)  # text方法也被 @property 伪装成属性了
    alertObject.accept()  # 点击确定按钮

    time.sleep(2)
    driver.find_element_by_id('alertButton').click()
    time.sleep(1)
    alertObject = driver.switch_to.alert
    time.sleep(2)
    alertObject.dismiss()  # 叉掉提示框

    time.sleep(2)
    driver.find_element_by_id('alertButton').click()
    time.sleep(1)
    alertObject = driver.switch_to.alert
    alertObject.send_keys('这一行会报错')  # selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: User dialog does not have a text box input field.


finally:
    time.sleep(10)
    driver.quit()

由最后的报错可以看到,alert提示框中不能使用alertObject.send_keys(message)方法。

selenium处理confirm提示框

回到顶部

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

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

driver = webdriver.Chrome()
try:

    driver.get(url='file:///M:/tests.html')
    time.sleep(2)
    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代码逻辑,当点击确定按钮后会再弹出一个提示框,我们再次点击确定
    time.sleep(2)
    driver.find_element_by_id('confirmButton').click()
    time.sleep(1)
    alertObject = driver.switch_to.alert
    alertObject.send_keys('这一行会报错')  # selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: User dialog does not have a text box input field.


finally:
    time.sleep(10)
    driver.quit()

同样的,confirm提示框中也不能使用alertObject.send_keys(message)方法。

selenium处理prompt提示框

回到顶部

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

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

driver = webdriver.Chrome()
try:

    driver.get(url='file:///M:/tests.html')
    time.sleep(2)
    driver.find_element_by_id('promptButton').click()
    time.sleep(1)
    alertObject = driver.switch_to.alert
    print(alertObject.text)  # 打印提示信息
    time.sleep(1)
    alertObject.send_keys('send keys msg')
    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('')
    time.sleep(1)
    alertObject.dismiss()  # 什么都不管,直接点击取消,会在弹出一个提示框
    time.sleep(1)
    # 现在弹出的是一个普通的提示框,点击确定和取消都无所谓,具体根据业务场景来决定
    # alertObject.accept()
    alertObject.dismiss()


finally:
    
    time.sleep(10)
    driver.quit()

在prompt提示框中,要看到input框才能send keys!

Guess you like

Origin www.cnblogs.com/Rxtong/p/11825982.html