python selenium 弹出框处理的实现


Posted in Python onFebruary 26, 2019

弹出框有两种:页面弹出框(可定位元素能操作)、Windows弹出框(不能直接定位)

一、页面弹出框

等待弹出框出现之后,定位弹出框,操作其中元素

如: 

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
#点击百度登录按钮
driver.find_element_by_xpath('//*[@id="u1"]//a[@name="tj_login"]').click()

#等待百度登录弹出框中 要出现的元素可见
ele_id = "TANGRAM__PSP_10__footerULoginBtn"
param = (By.ID,ele_id)
#元素可见时,再进行后续操作
WebDriverWait(driver,10).until(EC.visibility_of_element_located(param))

driver.find_element_by_id(ele_id).click()
time.sleep(5)
driver.quit()

二、Windows弹出框

使用 driver.switch_to.alert  切换到Windows弹出框

Alert类提供了一系列操作方法:

  • accept() 确定
  • dismiss() 取消
  • text() 获取弹出框里面的内容
  • send_keys(keysToSend) 输入字符串

如: 

#1:定位alert弹出框
#点击页面元素,触发alert弹出框
driver.find_element_by_xpath('//*[@id="alert"]').click()
time.sleep(3)
#等待alert弹出框可见
WebDriverWait(driver,20).until(EC.alert_is_present())

#从html页面切换到alert弹框 
alert = driver.switch_to.alert
#获取alert的文本内容
print(alert.text)
#接受--选择“确定”
alert.accept()

#2:定位confirm弹出框
driver.find_element_by_xpath('//*[@id="confirm"]').click()
time.sleep(3)
WebDriverWait(driver,20).until(EC.alert_is_present())
alert =driver.switch_to.alert
print(alert.text)
# 接受--选择“取消”
alert.dismiss()


#3:定位prompt弹出框
driver.find_element_by_id("prompt").click()
time.sleep(3)
WebDriverWait(driver,20).until(EC.alert_is_present())
alert =driver.switch_to.alert
alert.send_keys("jaja")
time.sleep(5)
print(alert.text)
# alert.dismiss()
alert.accept()

实例

首先复制下列的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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
总结Python编程中三条常用的技巧
May 11 Python
详解Python编程中time模块的使用
Nov 20 Python
简析Python的闭包和装饰器
Feb 26 Python
浅谈python 线程池threadpool之实现
Nov 17 Python
python3 发送任意文件邮件的实例
Jan 23 Python
Python模拟浏览器上传文件脚本的方法(Multipart/form-data格式)
Oct 22 Python
浅谈python下含中文字符串正则表达式的编码问题
Dec 07 Python
python将处理好的图像保存到指定目录下的方法
Jan 10 Python
python3 配置logging日志类的操作
Apr 08 Python
Python爬虫逆向分析某云音乐加密参数的实例分析
Dec 04 Python
python xlsxwriter模块的使用
Dec 24 Python
python实现银行账户系统
Feb 22 Python
python实现学员管理系统
Feb 26 #Python
python实现电子产品商店
Feb 26 #Python
Python selenium根据class定位页面元素的方法
Feb 26 #Python
python实现诗歌游戏(类继承)
Feb 26 #Python
Python实现简单查找最长子串功能示例
Feb 26 #Python
基于Python实现用户管理系统
Feb 26 #Python
python selenium firefox使用详解
Feb 26 #Python
You might like
PHP的类 功能齐全的发送邮件类
2006/10/09 PHP
php学习笔记 [预定义数组(超全局数组)]
2011/06/09 PHP
php去除换行符的方法小结(PHP_EOL变量的使用)
2013/02/16 PHP
PHP函数extension_loaded()用法实例
2015/01/19 PHP
javascript加号&quot;+&quot;的二义性说明
2013/03/04 Javascript
JS计算网页停留时间代码
2014/04/28 Javascript
JavaScript中的单引号和双引号报错的解决方法
2014/09/01 Javascript
Javascript调用函数方法的几种方式介绍
2015/03/20 Javascript
JS实现具备延时功能的滑动门菜单效果
2015/09/17 Javascript
薪资那么高的Web前端必看书单
2017/10/13 Javascript
在vue项目中使用sass的配置方法
2018/03/20 Javascript
React学习笔记之高阶组件应用
2018/06/02 Javascript
CountUp.js实现数字滚动增值效果
2019/10/17 Javascript
基于JQuery实现页面定时弹出广告
2020/05/08 jQuery
python中Pycharm 输出中文或打印中文乱码现象的解决办法
2017/06/16 Python
Python编程之字符串模板(Template)用法实例分析
2017/07/22 Python
Python 查看list中是否含有某元素的方法
2018/06/27 Python
把JSON数据格式转换为Python的类对象方法详解(两种方法)
2019/06/04 Python
selenium跳过webdriver检测并模拟登录淘宝
2019/06/12 Python
Python使用numpy模块实现矩阵和列表的连接操作方法
2019/06/26 Python
Django框架基础模板标签与filter使用方法详解
2019/07/23 Python
关于PyTorch源码解读之torchvision.models
2019/08/17 Python
使用Rasterio读取栅格数据的实例讲解
2019/11/26 Python
Python如何获取Win7,Win10系统缩放大小
2020/01/10 Python
Matplotlib 绘制饼图解决文字重叠的方法
2020/07/24 Python
Python使用socket_TCP实现小文件下载功能
2020/10/09 Python
美国著名的家居用品购物网站:Bed Bath & Beyond
2018/01/05 全球购物
澳大利亚在线消费电子产品商店:TobyDeals
2020/01/05 全球购物
现代化办公人员工作的自我评价
2013/10/16 职场文书
文化宣传方案
2014/03/13 职场文书
网络技术专业求职信
2014/07/13 职场文书
就业协议书
2014/09/12 职场文书
员工离职通知函
2015/04/25 职场文书
师德师风心得体会(2016精选篇)
2016/01/12 职场文书
公司年会主持词范文!
2019/05/07 职场文书
Vue CLI中模式与环境变量的深入详解
2021/05/30 Vue.js