首页 > 代码库 > 9. 处理alert/confirm/prompt

9. 处理alert/confirm/prompt

webdriver中处理原生的js alert confirm 以及prompt是很简单的。具体思路是使用switch_to.alert()方法定位到alert/confirm/prompt。然后使用text /accept/dismiss/send_keys按需进行操做

  • text。返回alert/confirm/prompt中的文字信息
  • accept。点击确认按钮
  • dismiss。点击取消按钮,如果有的话
  • send_keys。向prompt中输入文字

alert.html

<html>    <head>      <meta http-equiv="content-type" content="text/html;charset=utf-8" />      <title>alert</title>            <script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>      <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />              <script type="text/javascript">        $(document).ready(function(){          $(‘#tooltip‘).tooltip({"placement": "right"});          $(‘#tooltip‘).click(function(){              alert(‘watir-webdriver better than selenium-webdriver‘)          });        });      </script>    </head>    <body>      <div class="row-fluid">        <div class="span6 well">                  <h3>alert</h3>          <a id="tooltip" href="#" data-toggle="tooltip" title="watir-webdriver better than selenium-webdriver">hover to see tooltip</a>        </div>            </div>            </body>    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>  </html>
=====================================================================================================================================================

alert.py

# -*- coding: utf-8 -*- from selenium import webdriverfrom time import sleepimport osif ‘HTTP_PROXY‘in os.environ: del os.environ[‘HTTP_PROXY‘]dr = webdriver.Chrome()file_path = ‘file:///‘ + os.path.abspath(‘alert.html‘)dr.get(file_path)# 点击链接弹出alertdr.find_element_by_id(‘tooltip‘).click()try:    alert = dr.switch_to_alert()    alert.accept()except:    print ‘no alerts display‘sleep(1)dr.quit()