selenium的alert/confirm/prompt处理

prompt/alert/confirm不能直接通过定位工具进行获取,selenium处理js原生的提示信息prompt/alert/confirm使用switch_to_alert方法切换对象,和frame类似

alert\confirm\prompt 弹出框操作主要方法有:

text:获取文本值

accept() :点击"确认"

dismiss() :点击"取消"或者叉掉对话框

send_keys() :输入文本值 --仅限于 prompt,在 alert 和 confirm 上没有输入框

html 源码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dialogs:alert,confirm,prompt</title>
</head>
<body>
 <input id = "alert" value = "alert" type = "button" onclick = "alert('请按确认继续!');"/>  
 <input id = "confirm" value = "confirm" type = "button" onclick = "confirm('是否确定?');"/>  
 <input id = "prompt" value = "prompt" type = "button" onclick = "var name = prompt('请输入地址:',''); document.write(name) "/>  
</body>

</html>

测试代码:

#coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("file:///D:/1.html")
driver.find_element_by_id("alert").click()
Alert= driver.switch_to_alert()
print("alert text: "+Alert.text)
Alert.dismiss()
driver.implicitly_wait(5)

driver.find_element_by_id("confirm").click()
confirm= driver.switch_to_alert()
print("confirm text: "+confirm.text)
Alert.accept()
driver.implicitly_wait(5)

driver.find_element_by_id("prompt").click()
prompt= driver.switch_to_alert()
print("prompt text: "+prompt.text)
prompt.send_keys("世界末日!")
Alert.dismiss()
driver.implicitly_wait(5)


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页