一、什么是消息框

是指在网页里进行某些操作(点击按钮、点击超级链接等)后,在网页上一层出现弹出来的对话框,对话框里显示要提示给客户的消息。

二、如何判断是不是消息框

  1. 1. 在对话框上点击鼠标右键,无菜单。
  2. 2. 大多数都是模态的,如果不关闭它,后面的网页无法进行操作。

注意:消息框不同于网页里的div标记的元素,虽然div元素(代表网页里的一块区域)可以显示为浮动在其他区域上一层的效果,但是不具备上述两个特点。

三、消息框种类

类型 功能 方法
alerts 警告框 显示通知消息
只能选择OK
```

accept()

  1. |
  2. | | ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25474243/1670251049257-b594b851-6dca-47fa-ba6c-f2cd05700b51.png#averageHue=%23fdfdfd&clientId=u7e9d29ec-a21d-4&from=paste&height=205&id=uc75b9e76&originHeight=205&originWidth=682&originalType=binary&ratio=1&rotation=0&showTitle=false&size=11912&status=done&style=none&taskId=ue7d73745-b8b9-4435-aa67-561b731c491&title=&width=682) | |
  3. | **confirm 确认框** | 可以选择确认<br />可以选择取消 |

accept() dismiss()

  1. |
  2. | | ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25474243/1670251099473-1bba15e8-738e-48b7-a86a-3b1da6afeba6.png#averageHue=%23fdfdfd&clientId=u7e9d29ec-a21d-4&from=paste&height=202&id=u62af1819&originHeight=202&originWidth=678&originalType=binary&ratio=1&rotation=0&showTitle=false&size=11257&status=done&style=none&taskId=u69d038e0-7e43-46c8-9ce6-2ebc98bf18a&title=&width=678) | |
  3. | **prompt 提示框** | 可以输入信息然后选择确认<br />可以选择取消 |

send_keys()后accept() dismiss()

  1. |
  2. | | ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25474243/1670251111254-94d6e0fe-0598-47af-b87f-f9bef18c3739.png#averageHue=%23fefdfd&clientId=u7e9d29ec-a21d-4&from=paste&height=277&id=u6fec1b7b&originHeight=277&originWidth=683&originalType=binary&ratio=1&rotation=0&showTitle=false&size=15776&status=done&style=none&taskId=uef7cfbe9-3f05-4f9b-bade-dbb157bcdcb&title=&width=683) | |
  3. <a name="EkPKb"></a>
  4. ### 四、代码编写
  5. | **alert**<a name="sj7dh"></a>
  6. ### <br />
  7. | ```python
  8. from selenium import webdriver
  9. from time import sleep
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.support.wait import WebDriverWait as wait
  13. # 用这个网站也可以http://sahitest.com/demo/confirmTest.htm
  14. driver=webdriver.Chrome()
  15. driver.get('https://www.selenium.dev/zh-cn/documentation/webdriver/interactions/alerts/')
  16. driver.find_element(By.XPATH, "//*[text()='查看样例警告框']").click()
  17. # 判断是否有弹窗
  18. # 方法一:显示等待写法
  19. alert = wait(driver,20,0.5).until(EC.alert_is_present())
  20. sleep(2)
  21. #获取弹窗文本
  22. text = alert.text
  23. print(text)
  24. # 点击确认按钮
  25. alert.accept()

| | —- | —- | | confirm | ```python from selenium import webdriver from time import sleep from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait as wait

用这个网站也可以:http://sahitest.com/demo/promptTest.htm

driver=webdriver.Chrome() driver.get(‘https://www.selenium.dev/zh-cn/documentation/webdriver/interactions/alerts/‘) driver.find_element(By.XPATH, “//*[text()=’查看样例确认框’]”).click() wait(driver,20,0.5).until(EC.alert_is_present()) sleep(2)

方法二:switch_to.alert

alert=driver.switch_to.alert text = alert.text #获取alter的文本

点击确认

alert.accept()

点击取消

alert.dismiss()

  1. |
  2. | **prompt** | ```python
  3. from selenium import webdriver
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.wait import WebDriverWait as wait
  7. #导入Alert类
  8. from selenium.webdriver.common.alert import Alert
  9. from time import sleep
  10. # 用这个网站也可以:http://sahitest.com/demo/promptTest.htm
  11. driver=webdriver.Chrome()
  12. driver.get('https://www.selenium.dev/zh-cn/documentation/webdriver/interactions/alerts/')
  13. driver.find_element(By.XPATH, "//*[text()='查看样例提示框']").click()
  14. wait(driver,20,0.5).until(EC.alert_is_present())
  15. sleep(2)
  16. #方法三:导入并调用Alert类
  17. alert=Alert(driver)
  18. text = alert.text #获取alter的文本
  19. #向输入框输入一条文本
  20. alert.send_keys('这是一条测试数据!')
  21. # 点击确认
  22. alert.accept()
  23. # 点击取消
  24. # alert.dismiss()

| | 其他 | 非弹窗的内容,按照HTML元素处理即可 |