Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”
一、说明
在使用python3+selenium写自动升级程序的时侯,碰到一个弹出对话框需要点击确认的场景。弹出的对话框如下图所示。
对于弹框各种资料都说通过switch_to.alert属性获取对话框对象,然后使用该对象的accept()方法点击确认。
但使用该方法,一直报错:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”,截图如下:
二、对话框处理
2.1 基本消息框处理
JavaScript中有三种基本的消息:警告框、确认框、提示框;分别使用alert()、confirm()、prompt()弹出;形式依次如下:
对于这三种消息框,和网上大多数资料说的一样用如下几个方法进行处理:
# 定义浏览器
browser = webdriver.Firefox()
# 打开url
browser.get(url)
# 切换至消息框,适用于alert/confirm/prompt
alert_box = browser.switch_to.alert
# 点击消息框的确认按钮,返回值为true。适用于alert/confirm/prompt
alert_box.accept()
# 点击消息框的取消按钮,返回值为False。适用于confirm/prompt
alert_box.dismiss()
# 向输入框发送内容,适用于prompt
alert_box.send_keys("msg")
# 获取输入框内容,适用于prompt
alert_box.text
2.2 模态对话框处理
在理解中,非模态对话框是指不进行处理也能点击其他位置的对话框,模态对话框则是指不处理就不能点击页面其他位置的对话框。
按这定义,前面说的三种消息框也应该都算模态对话框;但我对html的一些术语不是很了解,反正我们这里就约定剔除前面三种弹出形式、然后又需要进行处理才能点击页面其他位置的对话框称为模态对话框。
在第一大节中使用switch_to.alert之所以报错,是因为该对话框并不是第二大节中所说的三种消息框,而是一个使用其他方法弹出的模态对话框(似乎是jquery框架弹出的,细节没研究);模态对话框不能通过switch_to.alert获取。
2.2.1 取巧方法----使用switch_to.active_element确认按钮然后点击
在第一大节中可以看到,“OK”按钮处于激活状态,我们正是要点击“OK”按钮,所以我们可以使用switch_to.active_element获取“OK”对象,然后进行点击。
# 切换至激活状态控件
element = browser.switch_to.active_element
# 调用click()方法点击该按钮
element.click()
2.2.2 通用的方法----直接通过find_element_by_id()等定位按钮然后点击
在2.2.1中介绍的方法不够通用,假如“OK”不处于激活状态或者我们要点击的是“Cancel”按钮,这种方法就行不通了。
在2.2.1介绍的三种消息对话框没有相应的html代码---即通过查看页面源代码看不到相应的html代码----但模态对话框在弹出时是有相应的html代码的(未弹出时没有),也就是说我们完全可以像获取普通控件一样,通过find_element_by_id()等方法获取控件对象。
如上可以看到“OK”控件id为“btnYO”,所以直接代码就是:
browser.find_element_by_id("btnYO").click()
三、更加轻巧的selenium代码书写方法
前面所说的两种方法基本可以处理我们如何关闭弹窗的问题,但一是我们要判断对话框是什么对话框,二是在判断为模态对话框之后我们还得进行点击查看源代码然后提取xpath等操作。
也许一次两次这样的操作工作量还可接受,但如果很多时就比较烦,我们可以借助更轻松的方法实现;这种方法其实不只对处理对话框,对使用selenium操作页面的代码的编写都是一种很有用的方法。
那就是使用Selenium IDE的代码导出功能,使用Selenium IDE记录整个点击过程后,将过程导出为Python等语言的代码,我们就可以轻松地知道想要的点击操作在代码上的实现;当然新版本的Selenium IDE导出代码已暂不可用(应该是Firefox等架构调整导致其原先代码不可用而新代码又没写好),但我们可以使用Katalon Recorder等代替品来实现。
当然,这也不是说以后你想写一个selenium操作页面的代码,都直接使用Katalon Recorder开启记录,然后把整个过程的操作一下,最后再导出代码就万事大吉了。主要是因为Katalon Recorder等并不能清楚地知道你进行下一步前的判断标准,也不知道异常之后你想进行的操作。比如你想等待一个按钮加载完可点击之后进行点击,如果不能点击就再等一下;但Katalon Recorder并不知道你的这个思想过程,他只觉得整个过程就是你等了5秒就点击了某个控件。即Katalon Recorder是一大助力但也只是一大助力而不是万能钥匙。
参考:
https://www.runoob.com/js/js-popup.html
Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”的更多相关文章
- 【Selenium】【BugList7】执行driver.find_element_by_id("kw").send_keys("Selenium"),报错:selenium.common.exceptions.InvalidArgumentException: Message: Expected [object Undefined] undefined to be a string
[版本] selenium:3.11.0 firefox:59.0.3 (64 位) python:3.6.5 [代码] #coding=utf-8 from selenium import webd ...
- windows下使用selenium报错selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH
问题 :执行程序代码报错: WebDriverException:Message:'geckodriver'executable needs to be in Path 或者 selenium.com ...
- 【Python + Selenium】初次用IE浏览器之报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones.
初次用IE浏览器运行自动化程序时,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launchi ...
- robotframework执行用例时,报错selenium.common.exceptions.WebDriverException: Message: unknown error: cannot get automation extension from unknown error: page could not be found: chrome-extension://aapnijgdinl
在用robotframework编写移动端测试用例(用chrome浏览器模拟手机浏览器),执行用例时, 报错selenium.common.exceptions.WebDriverException: ...
- selenium的报错信息:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
报错信息:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class ...
- python无法启动火狐浏览器且报错“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”
安装了python2,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...
- selenium使用报错“selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.”
安装了python3,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...
- 解决centos7下 selenium报错--unknown error: DevToolsActivePort file doesn't exist
解决centos7下 selenium报错--unknown error: DevToolsActivePort file doesn't exist 早上在linux下用selenium启动Chro ...
- python+selenium,打开浏览器时报selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH
有一年多没写web自动化了,今天搭建环境的时候报了一个常见错误,但是处理过程有点闹心,报错就是常见的找不到驱动<selenium.common.exceptions.WebDriverExcep ...
随机推荐
- express和express-generator搭建web应用
nodemon的安装使用 安装 npm i nodemon -D 配置 新建nodemon.json { "watch": "src/**/*.* } 修改package ...
- Vue – 基础学习(4):事件修饰符
Vue – 基础学习(3):事件修饰符
- webpack打包js文件
当输入 webpack 输入指令 npm run dev 后会自动启动一个浏览器 需要借鉴插件 open-browser-webpack-plugin 下载:npm install open-bro ...
- SpringBoot 定时任务篇
一. 基于注解@Scheduled默认为单线程,开启多个任务时,任务的执行时机会受上一个任务执行时间的影响. 1.创建定时器 使用SpringBoot基于注解来创建定时任务非常简单,只需几行代码便可完 ...
- linux 下 Google配置SwitchyOmega
本文是在linux配置shadowssocks中配置的,windows也可以 通过上一篇文章我们学会了如何科学上网, 但是我们使用SwitchyOmega时选择的是proxy的代理模式 就是说我们不 ...
- Keras 训练一个单层全连接网络的线性回归模型
1.准备环境,探索数据 import numpy as np from keras.models import Sequential from keras.layers import Dense im ...
- 可变lambda, lambda使用mutable关键字
关于lambda的捕获和调用 C++ primer上对可变lambda举的例子如下: size_t v1=42; auto f=[v1] () mutable{return ++v1; }; v1=0 ...
- 张兴盼-201871010131 《面向对象程序设计(java)》第六、七周学习总结
张兴盼-201871010131 <面向对象程序设计(java)>第六.七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...
- Linux—— 记录所有登陆用户的历史操作记录
前言 记录相应的人登陆服务器后,做了那些操作,这个不是我自己写的,因为时间久了,原作者连接也无法提供,尴尬. 步骤 history是查询当前连接所操作的命令,通过编写以下内容添加至/etc/profi ...
- 08-C#笔记-判读语句
同C++ 支持if.switch.?: 不同之处 1. switch case支持字符. 参考: http://www.runoob.com/csharp/csharp-switch.html htt ...