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网络编程学习笔记(一)
Jun 09 Python
约瑟夫问题的Python和C++求解方法
Aug 20 Python
学习python可以干什么
Feb 26 Python
Python 分发包中添加额外文件的方法
Aug 16 Python
在python中计算ssim的方法(与Matlab结果一致)
Dec 19 Python
Python实现桌面翻译工具【新手必学】
Feb 12 Python
scrapy数据存储在mysql数据库的两种方式(同步和异步)
Feb 18 Python
python3.6环境下安装freetype库和基本使用方法(推荐)
May 10 Python
python中元组的用法整理
Jun 15 Python
Python 通过正则表达式快速获取电影的下载地址
Aug 17 Python
python 如何上传包到pypi
Dec 24 Python
解决pytorch读取自制数据集出现过的问题
May 31 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 iconv 函数转gb2312的bug解决方法
2009/10/11 PHP
php实时倒计时功能实现方法详解
2017/02/27 PHP
Colortip基于jquery的信息提示框插件在IE6下面的显示问题修正方法
2010/12/06 Javascript
js判断浏览器类型的方法
2013/08/07 Javascript
JS使用parseInt解析数字实现求和的方法
2015/08/05 Javascript
js实现无限级树形导航列表效果代码
2015/09/23 Javascript
学习JavaScript设计模式之状态模式
2016/01/08 Javascript
分享JavaScript监听全部Ajax请求事件的方法
2016/08/28 Javascript
基于cssSlidy.js插件实现响应式手机图片轮播效果
2016/08/30 Javascript
Vue 2.0中生命周期与钩子函数的一些理解
2017/05/09 Javascript
AngularJS实现动态添加Option的方法
2017/05/17 Javascript
jQuery列表检索功能实现代码
2017/07/17 jQuery
微信小程序textarea层级过高(盖住其他元素)问题的解决办法
2019/03/04 Javascript
5分钟教你用nodeJS手写一个mock数据服务器的方法
2019/09/10 NodeJs
Vue v-text指令简单使用方法示例
2019/09/19 Javascript
功能完善的小程序日历组件的实现
2020/03/31 Javascript
Django1.7+python 2.78+pycharm配置mysql数据库教程
2014/11/18 Python
python常见排序算法基础教程
2017/04/13 Python
python 系统调用的实例详解
2017/07/11 Python
python文件名和文件路径操作实例
2017/09/29 Python
python使用标准库根据进程名如何获取进程的pid详解
2017/10/31 Python
解决Python 中英文混输格式对齐的问题
2018/07/16 Python
详解Python对JSON中的特殊类型进行Encoder
2019/07/15 Python
python线程优先级队列知识点总结
2021/02/28 Python
HTML5新特性之用SVG绘制微信logo
2016/02/03 HTML / CSS
全球酒店预订网站:Hotels.com
2016/08/10 全球购物
汇科协同Java笔试题
2012/03/31 面试题
服装采购员岗位职责
2014/03/15 职场文书
党在我心中演讲稿
2014/09/02 职场文书
房地产公司财务总监岗位职责
2015/04/03 职场文书
2015年感恩母亲节活动方案
2015/05/04 职场文书
商场圣诞节活动总结
2015/05/06 职场文书
垂直极限观后感
2015/06/08 职场文书
九不准学习心得体会
2016/01/23 职场文书
2016年少先队活动总结
2016/04/06 职场文书
导游词之蓬莱长岛
2019/12/17 职场文书