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 相关文章推荐
jupyter安装小结
Mar 13 Python
Python3使用requests登录人人影视网站的方法
May 11 Python
Python金融数据可视化汇总
Nov 17 Python
对python数据切割归并算法的实例讲解
Dec 12 Python
python学习——内置函数、数据结构、标准库的技巧(推荐)
Apr 18 Python
Python一行代码实现快速排序的方法
Apr 30 Python
Python中字符串List按照长度排序
Jul 01 Python
使用OpCode绕过Python沙箱的方法详解
Sep 03 Python
Python列表删除元素del、pop()和remove()的区别小结
Sep 11 Python
django实现支付宝支付实例讲解
Oct 17 Python
python编程进阶之类和对象用法实例分析
Feb 21 Python
Python hashlib模块的使用示例
Oct 09 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 ADODB使用方法集锦
2008/03/25 PHP
php echo 输出字符串函数详解
2010/05/13 PHP
PHP 截取字符串专题集合
2010/08/19 PHP
php字符比较函数similar_text、strnatcmp与strcasecmp用法分析
2014/11/18 PHP
php通过baihui网API实现读取word文档并展示
2015/06/22 PHP
PHP实现获取并生成数据库字典的方法
2016/05/04 PHP
php 多继承的几种常见实现方法示例
2019/11/18 PHP
使用jquery mobile做幻灯播放效果实现步骤
2013/01/04 Javascript
js实现禁止中文输入的方法
2015/01/14 Javascript
基于AngularJS实现页面滚动到底自动加载数据的功能
2015/10/16 Javascript
仿Angular Bootstrap TimePicker创建分钟数-秒数的输入控件
2016/07/01 Javascript
微信小程序 require机制详解及实例代码
2016/12/14 Javascript
Require.js的基本用法详解
2017/07/03 Javascript
javascript  删除select中的所有option的实例
2017/09/17 Javascript
基于js中的原型(全面讲解)
2017/09/19 Javascript
vue 自定义指令自动获取文本框焦点的方法
2018/08/25 Javascript
在Vue环境下利用worker运行interval计时器的步骤
2019/08/01 Javascript
[46:32]Fnatic vs OG 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
用TensorFlow实现戴明回归算法的示例
2018/05/02 Python
详解python 3.6 安装json 模块(simplejson)
2019/04/02 Python
python3 反射的四种基本方法解析
2019/08/26 Python
Tensorflow中的dropout的使用方法
2020/03/13 Python
python读取yaml文件后修改写入本地实例
2020/04/27 Python
基于python实现坦克大战游戏
2020/10/27 Python
英国知名的护肤彩妆与时尚配饰大型综合零售电商:Unineed
2016/11/21 全球购物
美国在线面料商店:Online Fabric Store
2018/07/26 全球购物
波兰在线儿童和婴儿用品零售商:pinkorblue
2019/06/29 全球购物
屈臣氏菲律宾官网:Watsons菲律宾
2020/06/30 全球购物
Prototype是怎么扩展DOM的
2014/10/01 面试题
日语专业毕业生自荐信
2013/11/11 职场文书
餐饮加盟计划书
2014/01/10 职场文书
最新奶茶店创业计划书
2014/01/25 职场文书
初中同学聚会感言
2014/02/11 职场文书
使用Selenium实现微博爬虫(预登录、展开全文、翻页)
2021/04/13 Python
Nginx stream 配置代理(Nginx TCP/UDP 负载均衡)
2021/11/17 Servers
Python+Selenium实现抖音、快手、B站、小红书、微视、百度好看视频、西瓜视频、微信视频号、搜狐视频、一点号、大风号、趣头条等短视频自动发布
2022/04/13 Python