博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
selenium 在网页上给出用户提示信息后自动消失
阅读量:4228 次
发布时间:2019-05-26

本文共 1091 字,大约阅读时间需要 3 分钟。

selenium 可以使用 driver.execute_script(js) 来执行部分JS代码。虽然 selenium 自动化测试工具本质上是模拟人的部分操作,但是它所具备的功能让他可以突破人的极限去做一些事情,甚至可以做到一些连人都做不到的事情。

比如说在验证码输入错误以后,给出用户重新输入的提示信息:

               

driver.find_element_by_xpath(login_button).click()  # 点击登录time.sleep(SHORT_PROTECTION_TIME)  # 等待片刻再去点击弹窗确认按钮while True:    try:        driver.switch_to.alert.accept() # 试图点击弹窗    except NoAlertPresentException:        break # 没有弹窗,表示验证码输入正确,退出循环    else: # 有弹窗并已经点掉,则给出提示并等待重新输入验证码        print("请在网页重新输入正确的验证码!")        js = """        var span = document.createElement('span');        span.innerHTML = '请在10秒内重新输入正确的验证码!';        span.style.position = 'absolute';        span.style.top = '49%';        span.style.left = '47%';        span.style.color = 'red';        span.style.fontSize = '20px';        document.body.appendChild(span);        setTimeout(function() {document.body.removeChild(span)},9000);        """        driver.execute_script(js)        time.sleep(LONG_PROTECTION_TIME / 2)  # 等待重新输入验证码        driver.find_element_by_xpath(login_button).click()  # 点击登录        time.sleep(SHORT_PROTECTION_TIME)  # 等待片刻再去点击弹窗确认按钮

 

转载地址:http://bdjqi.baihongyu.com/

你可能感兴趣的文章
Lightwave 3D Character Animation
查看>>
Presenting Windows Workflow Foundation
查看>>
Visual Basic. NET professional projects
查看>>
Palm & Pocket PC Programming
查看>>
Visual Studio Tools for Office : Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
查看>>
Pro JSP 2, Fourth Edition (Expert's Voice in Java)
查看>>
Microsoft SQL Server 2005 Reporting Services 2005
查看>>
User Mode Linux(R) (Bruce Perens Open Source)
查看>>
Enterprise JavaBeans 3.0 (5th Edition)
查看>>
Eclipse 3 Live
查看>>
Java P2P Unleashed: With JXTA, Web Services, XML, Jini, JavaSpaces, and J2EE
查看>>
Java(TM) Network Programming and Distributed Computing
查看>>
Java Cookbook
查看>>
Intelligent Agent Software Engineering
查看>>
Project Management Training
查看>>
Microsoft Reporting Services in Action
查看>>
Successful Software Development (2nd Edition)
查看>>
How to Design Programs: An Introduction to Programming and Computing
查看>>
Beginning Relational Data Modeling, Second Edition
查看>>
Winternals Defragmentation, Recovery, and Administration Field Guide
查看>>