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中使用sort()方法进行排序的简单教程
May 21 Python
Python字符串拼接、截取及替换方法总结分析
Apr 13 Python
python使用matplotlib绘制折线图教程
Feb 08 Python
python中WSGI是什么,Python应用WSGI详解
Nov 24 Python
Python实现简易Web爬虫详解
Jan 03 Python
对numpy 数组和矩阵的乘法的进一步理解
Apr 04 Python
Python不使用int()函数把字符串转换为数字的方法
Jul 09 Python
用Python实现大文本文件切割的方法
Jan 12 Python
python 并发编程 非阻塞IO模型原理解析
Aug 20 Python
python 求10个数的平均数实例
Dec 16 Python
浅谈pytorch池化maxpool2D注意事项
Feb 18 Python
python音频处理的示例详解
Dec 23 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
全国FM电台频率大全 - 28 甘肃省
2020/03/11 无线电
浅析php中常量,变量的作用域和生存周期
2013/08/10 PHP
PHP的构造方法,析构方法和this关键字详细介绍
2013/10/22 PHP
TP3.2.3框架使用CKeditor编辑器在页面中上传图片的方法分析
2019/12/31 PHP
javascript中类的定义及其方式(《javascript高级程序设计》学习笔记)
2011/07/04 Javascript
jQuery切换所有复选框选中状态的方法
2015/07/02 Javascript
javascript实现禁止复制网页内容汇总
2015/12/30 Javascript
Node.js如何自动审核团队的代码
2016/07/20 Javascript
浅谈JS之tagNaem和nodeName
2016/09/13 Javascript
JavaScript选取(picking)和反选(rejecting)对象的属性方法
2017/08/16 Javascript
js + css实现标签内容切换功能(实例讲解)
2017/10/09 Javascript
js 显示日期时间的实例(时间过一秒加1)
2017/10/25 Javascript
vue-cli3使用 DllPlugin 实现预编译提升构建速度
2019/04/24 Javascript
微信小程序扫描二维码获取信息实例详解
2019/05/07 Javascript
JavaScript检测浏览器是否支持CSS变量代码实例
2020/04/03 Javascript
[02:41]DOTA2亚洲邀请赛小组赛第三日 赛事回顾
2015/02/01 DOTA
[04:42]5分钟带你了解什么是DOTA2(第一期)
2017/02/07 DOTA
Python生成验证码实例
2014/08/21 Python
实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
2016/01/20 Python
Pycharm 设置自定义背景颜色的图文教程
2018/05/23 Python
Python 判断文件或目录是否存在的实例代码
2018/07/19 Python
pycharm恢复默认设置或者是替换pycharm的解释器实例
2018/10/29 Python
Python 从相对路径下import的方法
2018/12/04 Python
使用python爬取微博数据打造一颗“心”
2019/06/28 Python
python中的列表与元组的使用
2019/08/08 Python
Python Sympy计算梯度、散度和旋度的实例
2019/12/06 Python
利用matplotlib实现根据实时数据动态更新图形
2019/12/13 Python
中专药剂专业应届毕的自我评价
2013/12/27 职场文书
证婚人搞笑证婚词
2014/01/10 职场文书
演讲稿祖国在我心中
2014/05/04 职场文书
关于环保的宣传稿
2015/07/23 职场文书
校运会宣传稿大全
2015/07/23 职场文书
如何才能写好调研报告?
2019/07/03 职场文书
解读Vue组件注册方式
2021/05/15 Vue.js
Golang二维数组的使用方式
2021/05/28 Golang
Android中View.post和Handler.post的关系
2022/06/05 Java/Android