About

 

重新认识alert
首先,不是所有的alert都能叫做alert框。
JavaScript中,关于消息提示框的方法有三个(虽然都跟alert差不多):

  • 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如何处理?
selenium操作上面三种提示框有以下几种方法:

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

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!




About

 

重新认识alert
首先,不是所有的alert都能叫做alert框。
JavaScript中,关于消息提示框的方法有三个(虽然都跟alert差不多):

  • 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如何处理?
selenium操作上面三种提示框有以下几种方法:

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

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!