当前位置:网站首页>Selenium automatic pop-up processing
Selenium automatic pop-up processing
2022-04-22 19:54:00 【Peng Yuyan from the testing industry】
1. Preface
We are using Selenium do Web When automating tests , Pop ups often appear on the page , If you don't process the subsequent test script, it won't work properly , Today we will take you to learn how to deal with Web Pop up window on the page .
2.Web Category of pop-up window on page
Pop ups usually have 3 Kind of :Alert Type bullet frame 、Confirm Type bullet frame 、Prompt Type bullet frame , We can't do this by locating elements . Their specific characteristics are as follows :
-
Alert Popup : Only information and confirmation buttons

-
Confirm Popup : stay Alert The Cancel button is added to the pop-up window

-
Prompt Type bullet frame : stay Confirm The function of inputting text content is added on the basis of

3. Solution
selenium The following methods are provided to deal with pop ups , The specific solutions are as follows :
from selenium import webdriver
driver = webdriver.Chrome()
driver.switch_to.alert.accept() # determine 、 agree! ; All three pop-up windows can be used
driver.switch_to.alert.dismiss() # Cancel 、 Disagree ;confirm and prompt Use in pop-up window
title = driver.switch_to.alert.text # Print pop-up information
alert = driver.switch_to.alert # obtain alert object
alert.send_keys() #Prompt Enter the content in the pop-up window
Some pop ups are not browser alert window , It is html Elements , This dialog box only needs to be processed according to the conventional positioning page elements
4. Practical drill
import unittest
from selenium import webdriver
from time import sleep
class TestAlert(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
self.driver.maximize_window()
def tearDown(self):
self.driver.quit()
def test_alert(self):
self.driver.get(r'http://sahitest.com/demo/alertTest.htm')
self.driver.find_element('name','b1').click()
alert = self.driver.switch_to.alert
print(alert.text)
alert.accept()
sleep(2)
def test_confirm(self):
self.driver.get(r'http://sahitest.com/demo/confirmTest.htm')
self.driver.find_element('name','b1').click()
alert = self.driver.switch_to.alert
print(alert.text)
alert.accept()
# alert.dismiss()
sleep(2)
def test_prompt(self):
self.driver.get(r'http://sahitest.com/demo/promptTest.htm')
self.driver.find_element('name','b1').click()
alert = self.driver.switch_to.alert
print(alert.text)
alert.send_keys("CR77777777777777777")
sleep(1)
alert.accept()
sleep(2)
if __name__ == '__main__':
unittest.main()
Finally, thank everyone who reads my article carefully , Watching the rise and attention of fans all the way , Reciprocity is always necessary , Although it's not very valuable , If you can use it, you can take it

These materials , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful …….

版权声明
本文为[Peng Yuyan from the testing industry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221941206691.html
边栏推荐
- 为什么说PuddingSwap或许为成为GameFi领域的一匹强势黑马?
- Software testing industry must see, a text of 800 words to teach you how to build the allure test report environment
- 2018-8-10-win10-uwp-business game-1.2.1
- 工作光靠努力是不够的!还有这个...
- 10.4.4 experiment of 8 LED "running lights" in 51 single chip microcomputer control system
- Speak through object prototype. toString. call()
- Firewalld DBUS interface usage guide
- How can Xinhua 3 enable the whole stack of data center carbon neutralization?
- Learning Android VI from scratch -- data persistence
- Packaging PHP with rpmbuild
猜你喜欢

Household intelligent projector is the first choice, entering the polar meter h3s projection

Software testing industry must see, a text of 800 words to teach you how to build the allure test report environment

The origin explanation and use example of image pre training model

Format for creating Zimbra LDAP users using LDAP clients

Jmeter:一文2000字接口测试基础知识介绍

一文读懂Hoo Smart Chain的可视化公链

顶测科技整理转行过程中一定要避免的问题

. net learning notes (III) -- ubiquitous features

STM32 learning record 006 - new project template (based on firmware library)

Linux下安装Mysql 8.0 + eclipse配置详细教程
随机推荐
2022-01-12 微信小程序调试
Problems that must be avoided in the process of top survey technology sorting and transformation
Learning Android VI from scratch -- data persistence
From functional test to automatic test, the salary doubled. I sorted out this 3000 word super complete learning guide
Format for creating Zimbra LDAP users using LDAP clients
Is it safe to open futures account on mobile phone? Do you need the assistance of offline account manager?
国内期货开户到哪个期货公司开更安全?
Selenium自动化之弹窗处理
《PyTorch深度学习实践》09 多分类问题
光刻机巨头ASML爆料:芯片太缺,都开始拆洗衣机了!
Why is x16 slower than X8?
Lithography giant ASML broke the news: the chip is too short, and they are starting to dismantle the washing machine!
Time format of go language
[渝粤教育] 四川农业大学 管理学原理 参考 资料
Hard work alone is not enough! And this
EasyCVR流媒体内核无法启动是什么原因?
软件测试入行必看,一文800字教你Allure 测试报告环境搭建
CVPR 2022: is smile recognition also sexist? Zhejiang University and Wuhan University jointly set up a fairness improvement framework
dbus客户端使用指南
Chrome plug-in dark reader, eye protecting dark mode browser