selenium对弹窗(alert)的处理

8 篇文章 0 订阅
4 篇文章 2 订阅

弹窗

selenium提供 switch_to_alert方法:捕获弹出对话框(可以定位alert、confirm、prompt对话框)

switch_to_alert()定位弹出对话框
text()获取对话框文本值
accept()相当于点击“确认”
dismiss()相当于点击“取消”
send_keys()输入值(alert和confirm没有输入对话框,所以就不用能用了,只能使用在prompt里)

测试HTML代码:

示例一:

<html>
    <head>
        <title>Alert</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>

示例二:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title></title>
    </head>
    <body>
        <div align="center">
        <h4>hello girl</h4>
        <input type="button" οnclick="showPro()" value="输入框弹窗按钮"/>
        <input type="button" οnclick="showAlert2()" value="提示弹窗按钮"/>
        <input type="button" οnclick="showAlert()" value="确认弹窗按钮"/>
 
 
 
        <span id="textSpan"></span>
        
        </div>
    </body>
    <script>
        function showAlert(){            
            document.getElementById("textSpan").innerHTML="";
            if(confirm("你是帅哥吗?")){
                document.getElementById("textSpan").innerHTML="<font style='color: red;'>您为何如此自信?</font>";
            }else{
                document.getElementById("textSpan").innerHTML="<font style='color: red;'>您为何如此谦虚?</font>";
            }
            
        }
        function showPro(){
            document.getElementById("textSpan").innerHTML="";
            con = prompt("输入1为强哥聪明,输入2为左哥笨");
            if(con==1){
                document.getElementById("textSpan").innerHTML="<font style='color: green;'>强哥是真聪明啊</font>";
            }else if(con==2){
                document.getElementById("textSpan").innerHTML="<font style='color: green;'>左哥是真笨啊</font>";
            }else{
                document.getElementById("textSpan").innerHTML="<font style='color: red;'>您没有按要求输入,请重新输入</font>";
            }
        }
        function showAlert2(){
            document.getElementById("textSpan").innerHTML="";
            alert("用我三世烟火,换你一世迷离");
        }
    </script>
</html>

1、alert窗口处理

import time
from selenium import webdriver
 
"""
处理alert弹窗
"""
driver = webdriver.Chrome('F:\PyCharmProject\TestFramework\drivers\chromedriver.exe')
driver.get('file:///C:/Users/Uker/Desktop/seleniumHTML/alert.html')
time.sleep(1)
# 获取alert对话框的按钮,点击按钮,弹出alert对话框
driver.find_element_by_id('alert').click()
time.sleep(1)
# 获取alert对话框
dig_alert = driver.switch_to.alert
time.sleep(1)
# 打印警告对话框内容
print(dig_alert.text)
# alert对话框属于警告对话框,我们这里只能接受弹窗
dig_alert.accept()
time.sleep(1)
 
driver.quit()

2、confirm窗口处理

import time
from selenium import webdriver
 
"""
处理confirm对话框
"""
# 获取confirm对话框的按钮,点击按钮,弹出confirm对话框
driver.find_element_by_id('confirm').click()
time.sleep(1)
# 获取confirm对话框
dig_confirm = driver.switch_to.alert
time.sleep(1)
# 打印对话框的内容
print(dig_confirm.text)
# 点击“确认”按钮
dig_confirm.accept()
# 点击“取消”按钮
# dig_confirm.dismiss()
time.sleep(1)
driver.quit()

3、prompt窗口处理

import time
from selenium import webdriver
 
"""
处理prompt对话框
"""
# 获取prompt对话框的按钮,点击按钮,弹出prompt对话框
driver.find_element_by_id('prompt').click()
time.sleep(1)
# 获取prompt对话框
dig_prompt = driver.switch_to.alert
time.sleep(1)
# 打印对话框内容
print(dig_prompt.text)
# 在弹框内输入信息
dig_prompt.send_keys("Loading")
# 点击“确认”按钮,提交输入的内容
dig_prompt.accept()
time.sleep(1)
driver.quit()

原文链接:https://blog.csdn.net/lykio_881210/article/details/80915882

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值