Python Selenium:浏览器弹出框处理

在操作浏览器的时候,会经常遇到浏览器的警告弹窗。一般的弹窗分为三种:1.警告类弹alert(),显示警告或其他信息,用于通知用户,下方只有一个【确认】按钮。2.确认类弹窗confirm(),询问是否继续某种操作等功能,下方有【确认】和【取消】两种按钮。3.消息类弹窗prompt(),需要输入一些信息,比如用户密码等,下方会有【确认】和【取消】按钮。

Alert类

Selenium针对浏览器Alert也有一些相应的API来处理,我们先来看下面的介绍。

名称用法
accept()点击Alert的【确认】按钮
authenticate(username,password)给需要验证的Alert发送账号和密码,默认点击OK
dismiss()点击Alert的【取消】按钮
send_keys(keysToSend)在Alert的输入框输入信息
text获取Alert上的文言信息
switch_to.alert切换到Alert

对Alert操作有两种方法,可以使用switch_to.alert,切换到Alert,或者通过Alert(driver)来使用,我们来看下面的实际操作。

实例

首先复制下列的html代码,保存为test.html到与脚本相同的文件夹下。这个html文件包含三个按钮,点击后会弹出三种不同的弹出框,另外还有一个文字区域,显示刚才的动作。

<!doctype html>
<head>
    <title>alert,confirm and prompt</title>
    <script type='text/javascript'>
        function myFunctionAlert(){
            window.alert('this is an alert, it has a confirm button')
            document.getElementById('action').value = 'you just clicked confirm button of alert()'
        }

        function myFunctionConfirm(){
            var result = window.confirm('this is a confirm,it has a confirm button and a cancel button')
            if(result == true){
                document.getElementById('action').value = 'you just clicked confirm button of confirm()'
            }else if(result == false){
                document.getElementById('action').value = 'you just clicked cancel button of confirm()'
            }else{
                document.getElementsById('action').value = 'you did nothing'
            }
        }

        function myFunctionPrompt(){
           var result = window.prompt('this is a prompt,it has an input and two buttons')
           if(result == null){
               document.getElementById('action').value = 'you just clicked cancel button of prompt()'
           }else if(result == ''){
               document.getElementById('action').value = 'you just input nothing and clicked confirm button of prompt()'
           }else{
               document.getElementById('action').value = 'you just input \'' + result + '\' and clicked confirm button of promt()'
           }
        }
    </script>
    <body>
        <br>
        <button type='button' onclick='myFunctionAlert()'>show alert</button>
        <br>
        <button type='button' onclick='myFunctionConfirm()'>show confirm</button>
        <br>
        <button type='button' onclick='myFunctionPrompt()'>show prompt</button>
        <br>
        <textarea id='action' style="width:200px;height:100px;font-family: Microsoft YaHei"></textarea>
    </body>
</head>

首先我们先实现:
1.点击第一个按钮‘show alert’,然后在弹出的对话框中点击【确认】按钮,并且打印你的动作。
2.点击第二个按钮‘show confirm’,然后在弹出的对话框中点击【取消】按钮,并且打印你的动作。

# -*- coding: utf-8 -*-

from selenium import webdriver
from time import sleep
import os

driver = webdriver.Chrome()
driver.implicitly_wait(10)
file = 'file:///' + os.path.abspath('test.html')
driver.get(file)

driver.find_element_by_css_selector('body>button:nth-child(2)').click() #使用css选择器定位,show alert按钮为body下的第二个子元素
sleep(2)
alert = driver.switch_to.alert #切换到alert
print('alert text : ' + alert.text) #打印alert的文本
alert.accept() #点击alert的【确认】按钮
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value')) #打印刚才的操作(获取页面最下方的textarea中文本)
sleep(2)
driver.find_element_by_css_selector('body>button:nth-child(4)').click()
sleep(2)
confirm = driver.switch_to.alert
print('confirm text : ' + confirm.text) #打印confirm的文本
confirm.dismiss() #点击confirm的取消按钮
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value'))
sleep(2)
driver.quit()

接着我们来操作:点击第三个按钮‘show prompt’,输入文字后点击【确认】按钮。

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.alert import Alert #导入Alert包
from time import sleep
import os

driver = webdriver.Chrome()
driver.implicitly_wait(10)
file = 'file:///' + os.path.abspath('test.html')
driver.get(file)

driver.find_element_by_css_selector('body>button:nth-child(6)').click()
sleep(2)
prompt = Alert(driver) #实例Alert对象,但使用时前面一定要导入Alert包
print('prompt text : ' + prompt.text) #打印promt的文言
prompt.send_keys('test prompt') #发送信息到输入框中
sleep(2)
prompt.accept() #点击【确认】按钮
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value')) #打印刚才的操作
sleep(2)
driver.quit()


欢迎订阅我的公众号:进击的小QA,第一时间收到文章推送哦

微信

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值