selenium对弹窗(alert)的处理

1、弹窗
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" onclick="showPro()" value="输入框弹窗按钮"/>
        <input type="button" onclick="showAlert2()" value="提示弹窗按钮"/>
        <input type="button" onclick="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()

猜你喜欢

转载自blog.csdn.net/lykio_881210/article/details/80915882
今日推荐