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群发邮件实例代码
Jan 03 Python
Python中的rfind()方法使用详解
May 19 Python
Django实现快速分页的方法实例
Oct 22 Python
Python爬虫中urllib库的进阶学习
Jan 05 Python
Python中反射和描述器总结
Sep 23 Python
python 实现倒排索引的方法
Dec 25 Python
python爬虫项目设置一个中断重连的程序的实现
Jul 26 Python
在Django下测试与调试REST API的方法详解
Aug 29 Python
浅谈keras中的Merge层(实现层的相加、相减、相乘实例)
May 23 Python
anaconda3安装及jupyter环境配置全教程
Aug 24 Python
python mock测试的示例
Oct 19 Python
Django数据库迁移常见使用方法
Nov 12 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循环结构实例讲解
2014/02/10 PHP
推荐一款PHP+jQuery制作的列表分页的功能模块
2014/10/14 PHP
10款PHP开源商城系统汇总介绍
2015/07/23 PHP
Windows2003下php5.4安装配置教程(IIS)
2016/06/30 PHP
利用PHP如何写APP接口详解
2016/08/23 PHP
php分享朋友圈的实现代码
2019/02/18 PHP
javascript 页面划词搜索JS
2009/09/28 Javascript
juqery 学习之三 选择器 可见性 元素属性
2010/11/25 Javascript
html+js实现动态显示本地时间
2013/09/21 Javascript
JavaScript动态设置div的样式的方法
2015/12/26 Javascript
在javascript中创建对象的各种模式解析
2016/05/16 Javascript
全面解析Bootstrap中form、navbar的使用方法
2016/05/30 Javascript
jquery获取input type=text中的值的各种方式(总结)
2016/12/02 Javascript
jQuery中的一些小技巧
2017/01/18 Javascript
angularjs点击图片放大实现上传图片预览
2017/02/24 Javascript
JS实现的全排列组合算法示例
2017/10/09 Javascript
JavaScript继承与多继承实例分析
2018/05/26 Javascript
koa源码中promise的解读
2018/11/13 Javascript
Vue源码解析之Template转化为AST的实现方法
2018/12/14 Javascript
Node.js系列之安装配置与基本使用(1)
2019/08/30 Javascript
JavaScript对象属性操作实例解析
2020/02/04 Javascript
[04:45]上海特级锦标赛主赛事第三日TOP10
2016/03/05 DOTA
[01:10:57]Liquid vs OG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
浅谈python中的占位符
2017/11/09 Python
python http接口自动化脚本详解
2018/01/02 Python
python tornado修改log输出方式
2019/11/18 Python
Keras使用tensorboard显示训练过程的实例
2020/02/15 Python
Python2.6版本pip安装步骤解析
2020/08/17 Python
美国最便宜的旅游网站:CheapTickets
2017/07/09 全球购物
讲党性心得体会
2014/09/03 职场文书
医药公司开票员岗位职责
2015/04/15 职场文书
工作失职自我检讨书
2015/05/05 职场文书
2015初一年级组工作总结
2015/07/24 职场文书
先进工作者主要事迹材料
2015/11/03 职场文书
九年级历史教学反思
2016/02/19 职场文书
解决pycharm下载库时出现Failed to install package的问题
2021/09/04 Python