您的位置:首页 > 编程语言 > Python开发

Python脚本控制的WebDriver 常用操作 <二十二> 处理alert / confirm / prompt

2013-12-18 21:29 791 查看

测试用例场景

  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中输入文字

Python脚本

测试用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('Python-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="Python-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>


测试用Python脚本:

# coding=gbk
'''
Created on 2013年12月18日

@author: Administrator
'''
from selenium import webdriver
from time import sleep
import os
if 'HTTP_PROXY' in os.environ: del os.environ['HTTP_PROXY']

dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('alert.html')
dr.get(file_path)

dr.find_element_by_id('tooltip').click()

try:
alert = dr.switch_to_alert()
alert.accept()
except:
print 'no alerts display'

sleep(5)
dr.quit()


其他的方法以后再写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: