代码。
prompt.xml
例4:
<html>
<head>
<script type="text/javascript">
function disp_prompt()
{
var name=prompt("请问:您关注我的博客了吗?")
}
</script>
</head>
<body>
<center>
<input id="mouse" type="button" onclick="disp_prompt()" value="点我,点我有惊喜" />
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
</center>
</body>
</html>
run.py
# coding=gbk
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 获取浏览器对象
driver = webdriver.Chrome()
# 设置浏览器窗口大小
driver.maximize_window()
# 进入页面
driver.get('D:\Document\pythonDC\Python3\定位\Promp.html')
time.sleep(2)
# 点击按钮并弹出对话框
driver.find_element(By.ID, "mouse").click()
time.sleep(1)
# 定位对话框
promp = driver.switch_to.alert
print(promp)
# 打印对话框内容
print(promp.text)
# 发送文字至对话框
promp.send_keys("不怕猫的耗子A")
time.sleep(2)
# 点击对话框[确定]按钮以关闭对话框
# alter.accept()
# 点击对话框[取消]按钮以关闭对话框
promp.dismiss()
time.sleep(1)
# 关闭对话框后继续操作页面
driver.find_element(By.ID, "kw").send_keys("不怕猫的耗子A")
运行时# 发送文字至对话框 promp.send_keys("不怕猫的耗子A")
没有起作用