webdriver中处理原生的js alert confirm 以及prompt是很简单的。具体思路是使用switch_to.alert()方法定位到alert/confirm/prompt。然后使用text /accept/dismiss/send_keys按需进行操做
<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>
=====================================================================================================================================================
# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
import os
if ‘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)
# 点击链接弹出alert
dr.find_element_by_id(‘tooltip‘).click()
try:
alert = dr.switch_to_alert()
alert.accept()
except:
print ‘no alerts display‘
sleep(1)
dr.quit()
9. 处理alert/confirm/prompt,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/hugh007/p/3862936.html