一、處理模態窗口:showModalDialog
由於彈出模態窗口后,就無法定位到當前窗口的元素和模態窗口的元素,需要添加js解決
模態窗口動作類似下面語句:
<input id="ctl00_CPH_TopAddButton" class="LongOrangeButtonStyle" type="button" value="Add Single Location" onclick="javascript:window.showModalDialog('EditLocation.aspx?EditOrAdd=Add',window,'dialogWidth:450px;dialogHeight:600px;status:no; directories:yes;scrollbars:no;Resizable=no;status:no');RefreshLocation();" name="ctl00$CPH$TopAddButton">
此時,在生成模態窗口前, 先執行語句:
| storeEval | if(selenium.browserbot.getCurrentWindow().showModalDialog){selenium.browserbot.getCurrentWindow().showModalDialog = function(sURL,vArguments,sFeatures){selenium.browserbot.getCurrentWindow().open(sURL, 'modal', sFeatures);};} | xx |
再點擊
| click | id=ctl00_CPH_TopAddButton |
則會以新頁面形式彈出,就不會定位不到元素
操作完新頁面后,執行下面2個語句,回到主窗口。
| close | ||
| selectWindow | null |
二、處理confirm/alert/prompts彈出窗口
處理方式如下:

相應示例代碼如下:
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>主窗口</title>
<script type="text/javascript">
function output(resultText){
document.getElementById('output').childNodes[0].nodeValue=resultText;
}
function show_confirm(){
var confirmation=confirm("Choose an option.");
if (confirmation==true){
output("Confirmed.");
}
else{
output("Rejected!");
}
}
function show_alert(){
alert("I'm blocking!");
output("Alert is gone.");
}
function show_prompt(){
var response = prompt("What's the best web QA tool?","Selenium");
output(response);
}
</script>
</head>
<body>
<input type="button" id="btnConfirm" onclick="show_confirm()" value="Show confirm box" /></br>
<input type="button" id="btnAlert" onclick="show_alert()" value="Show alert" /></br>
<input type="button" id="btnPrompt" onclick="show_prompt()" value="Show prompt" /> </br>
<br />
<span id="output">
</span>
</body>
</html>
