(selenium+python)_UI自动化09_定位alert弹窗
Posted 测试一枝花
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(selenium+python)_UI自动化09_定位alert弹窗相关的知识,希望对你有一定的参考价值。
前言
web网站中某些点击操作会触发alert弹窗。在selenium进行自动化过程中,模拟操作alert弹窗时直接定位会报错找不到元素。需要先切换到alert然后用alert处理方法进行操作。
alert常用方法
switch_to.alert # 切换到alert弹窗
text # 获取弹出框文本
accept() # 确定
dismiss() # 取消
send_keys() # 输入
实例
模拟操作alert步骤如下:
点击某按钮弹出alert-->切换到alert-->操作alert
1 driver.find_element_by_id("").click() # 点击按钮,弹出alert弹窗
2 time().sleep(3) # 等待弹出alert
3 alt = driver.switch_to.alert
4 print("alert中文本:", alt.text) # 打印alert中文本
5 alt.accept() # 点击alert弹窗-确定btn
!--5f39ae17-8c62-4a45-bc43-b32064c9388a:W3siYmxvY2tJZCI6IjU2NzYtMTU1MzU2NjQwNzY3NSIsImJsb2NrVHlwZSI6InBhcmFncmFwaCIsInN0eWxlcyI6eyJhbGlnbiI6ImxlZnQiLCJpbmRlbnQiOjAsInRleHQtaW5kZW50IjowLCJsaW5lLWhlaWdodCI6MS43NSwiYmFjay1jb2xvciI6IiIsInBhZGRpbmciOiIifSwidHlwZSI6InBhcmFncmFwaCIsInJpY2hUZXh0Ijp7ImRhdGEiOlt7ImNoYXIiOiLngrkifSx7ImNoYXIiOiLlh7sifSx7ImNoYXIiOiLmn5AifSx7ImNoYXIiOiLmjIkifSx7ImNoYXIiOiLpkq4ifSx7ImNoYXIiOiLlvLkifSx7ImNoYXIiOiLlh7oifSx7ImNoYXIiOiJhIn0seyJjaGFyIjoibCJ9LHsiY2hhciI6ImUifSx7ImNoYXIiOiJyIn0seyJjaGFyIjoidCJ9LHsiY2hhciI6Ii0ifSx7ImNoYXIiOiItIn0seyJjaGFyIjoiPiJ9LHsiY2hhciI6IuWIhyJ9LHsiY2hhciI6IuaNoiJ9LHsiY2hhciI6IuWIsCJ9LHsiY2hhciI6ImEifSx7ImNoYXIiOiJsIn0seyJjaGFyIjoiZSJ9LHsiY2hhciI6InIifSx7ImNoYXIiOiJ0In0seyJjaGFyIjoiLSJ9LHsiY2hhciI6Ii0ifSx7ImNoYXIiOiI+In0seyJjaGFyIjoi5omTIn0seyJjaGFyIjoi5Y2wIn0seyJjaGFyIjoiYSJ9LHsiY2hhciI6ImwifSx7ImNoYXIiOiJlIn0seyJjaGFyIjoiciJ9LHsiY2hhciI6InQifSx7ImNoYXIiOiLmoYYifSx7ImNoYXIiOiLlhoUifSx7ImNoYXIiOiLmlocifSx7ImNoYXIiOiLmnKwifSx7ImNoYXIiOiLkv6EifSx7ImNoYXIiOiLmga8ifSx7ImNoYXIiOiItIn0seyJjaGFyIjoiLSJ9LHsiY2hhciI6Ij4ifSx7ImNoYXIiOiLngrkifSx7ImNoYXIiOiLlh7sifSx7ImNoYXIiOiLnoa4ifSx7ImNoYXIiOiLlrpoifSx7ImNoYXIiOiIvIn0seyJjaGFyIjoi5Y+WIn0seyJjaGFyIjoi5raIIn1dLCJpc1JpY2hUZXh0Ijp0cnVlLCJrZWVwTGluZUJyZWFrIjp0cnVlfX1d-->以上是关于(selenium+python)_UI自动化09_定位alert弹窗的主要内容,如果未能解决你的问题,请参考以下文章
(selenium+python)_UI自动化10_cookie常用操作
(selenium+python)_UI自动化03_定位新开窗口内元素
(selenium+python)_UI自动化04_定位iframe框架内元素
(selenium+python)_UI自动化05_定位select下拉列表
基于python实现UI自动化1.0 selenium工具介绍
Python+Selenium进行UI自动化测试项目中,常用的小技巧3:写入excel表(python,xlsxwriter)