文章目录
一、js的三种弹出框(alert、confirm、prompt)1、alert()2、confirm()3、prompt()
二、处理方式:1、方法2、示例HTML3、弹窗操作
一、js的三种弹出框(alert、confirm、prompt)
1、alert()
alert 方法只有一个参数,就是用户想弹出的内容。可以点击“确认”关闭弹出框。 例如:alert(“alert hello”)
2、confirm()
confirm方法需要用户选择,有确认有返回,返回值为 true 或 false。 例如: confirm(“confirm hello”)点击确定flag的值为true;点击取消则为false。
3、prompt()
prompt方法的弹出框可以直接进行回复。 例如:prompt(“prompt hello”) 如果输入的是:123456,点击确定之后值为:123456;点击取消后值为null。
二、处理方式:
先定位(switch_to.alert自动获取当前弹窗),再使用 text、accept、dismiss、send_keys 等方法进行操作。
1、方法
text 获取弹窗中的文字
accept 接受(确认)弹窗内容
dismiss 解除(取消)弹窗
send_keys 发送文本至警告框
2、示例HTML
这里写一个简单的测试页面,其中包含三个按钮,分别对应三个弹窗。
const dom1 = document.getElementById("alert")
dom1.addEventListener('click', function(){
alert("alert hello")
})
const dom2 = document.getElementById("confirm")
dom2.addEventListener('click', function(){
confirm("confirm hello")
})
const dom3 = document.getElementById("prompt")
dom3.addEventListener('click', function(){
prompt("prompt 请输入内容")
})
3、弹窗操作
使用上面的方法进行测试。
from selenium import webdriver
from pathlib import Path
from time import sleep
driver = webdriver.Firefox()
driver.get('file:///' + str(Path(Path.cwd(), '弹窗.html')))
sleep(2)
# 点击alert按钮
driver.find_element_by_xpath('//*[@id="alert"]').click()
sleep(1)
alert = driver.switch_to.alert
# 打印alert弹窗的文本
print(alert.text)
# 确认
alert.accept()
sleep(2)
# 点击confirm按钮
driver.find_element_by_xpath('//*[@id="confirm"]').click()
sleep(1)
confirm = driver.switch_to.alert
print(confirm.text)
# 取消
confirm.dismiss()
sleep(2)
# 点击confirm按钮
driver.find_element_by_xpath('//*[@id="prompt"]').click()
sleep(1)
prompt = driver.switch_to.alert
print(prompt.text)
# 向prompt的输入框中传入文本
prompt.send_keys("Dream丶Killer") #数据无法输入
sleep(2)
prompt.accept()
相关链接
发表评论