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编写android截屏脚本双击运行即可
Jul 21 Python
在Python中处理列表之reverse()方法的使用教程
May 21 Python
Python实现快速排序算法及去重的快速排序的简单示例
Jun 26 Python
Python中生成Epoch的方法
Apr 26 Python
python中lambda()的用法
Nov 16 Python
在python 中实现运行多条shell命令
Jan 07 Python
python3实现小球转动抽奖小游戏
Apr 15 Python
python射线法判断检测点是否位于区域外接矩形内
Jun 28 Python
浅谈Python3识别判断图片主要颜色并和颜色库进行对比的方法
Oct 25 Python
django框架中间件原理与用法详解
Dec 10 Python
基于python实现微信好友数据分析(简单)
Feb 16 Python
学点简单的Django之第一个Django程序的实现
Feb 24 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
Zerg剧情介绍
2020/03/14 星际争霸
openflashchart 2.0 简单案例php版
2012/05/21 PHP
PHP的反射类ReflectionClass、ReflectionMethod使用实例
2014/08/05 PHP
PHP实现图片上传并压缩
2015/12/22 PHP
php 类中的常量、静态属性、非静态属性的区别
2017/04/09 PHP
推荐17个优美新鲜的jQuery的工具提示插件
2012/09/14 Javascript
jquery入门—数据删除与隔行变色以及图片预览
2013/01/07 Javascript
js根据给定的日期计算当月有多少天实现思路及代码
2013/02/25 Javascript
利用JavaScript实现新闻滚动效果(实例代码)
2013/11/27 Javascript
javascript中定义类的方法详解
2015/02/10 Javascript
深入理解vue $refs的基本用法
2017/07/13 Javascript
用vue封装插件并发布到npm的方法步骤
2017/10/18 Javascript
微信小程序实战篇之购物车的实现代码示例
2017/11/30 Javascript
微信小程序实现流程进度的图样式功能
2018/01/16 Javascript
基于vue开发的在线付费课程应用过程
2018/01/25 Javascript
vue操作下拉选择器获取选择的数据的id方法
2018/08/24 Javascript
vue 解决数组赋值无法渲染在页面的问题
2019/10/28 Javascript
JS如何实现动态添加的元素绑定事件
2019/11/12 Javascript
ckeditor一键排版功能实现方法分析
2020/02/06 Javascript
Python使用爬虫猜密码
2016/02/19 Python
基于python元祖与字典与集合的粗浅认识
2017/08/23 Python
Php多进程实现代码
2018/05/07 Python
eclipse创建python项目步骤详解
2019/05/10 Python
python return逻辑判断表达式实现解析
2019/12/02 Python
Selenium启动Chrome时配置选项详解
2020/03/18 Python
python中 _、__、__xx__()区别及使用场景
2020/06/30 Python
html5定位获取当前位置并在百度地图上显示
2014/08/22 HTML / CSS
德国百年厨具品牌WMF美国站:WMF美国
2016/09/12 全球购物
英国DIY汽车维修配件网站:DIY Car Service Parts
2019/08/30 全球购物
类如何去实现接口
2013/12/19 面试题
法务专员岗位职责
2014/01/02 职场文书
冰淇淋开店创业计划书
2014/02/01 职场文书
单位员工收入证明样本
2014/10/09 职场文书
防汛通知
2015/04/25 职场文书
十大最强火系宝可梦,喷火龙上榜,第一名有双火属性
2022/03/18 日漫
Redis 哨兵机制及配置实现
2022/03/25 Redis