我无法找到在iframe内部点击确认弹出窗口的方法。我可以找到iframe并点击"点击确定"按钮,然后确认按钮弹出,此时我卡住了。我在下面发布了我的html和c#代码。
这是file:///C/Temp/IFrame.html
<html>
<body>
<iframe id="FrameID" src="file:///C:/Temp/confirm1.html" width="100%" height="100%">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
这里是 file:/// C:/Temp/confirm1.html
<html>
<body>
<input id="myButton1" type="button" value="this is a button"
onclick="confirmMe(); return false;"><br>
<script>
function confirmMe() {
var answer = confirm ("Are you having fun?")
if (answer)
document.getElementById("myButton1").value="Clicked OK";
else
document.getElementById("myButton1").value="Clicked Cancel";
}
</script>
</body>
</html>
以下是它的外观:
我的代码在没有框架的情况下有效,但是当使用框架时,我无法找到一种方法来点击从iframe加载的确认对话框。
public static void ConfirmPopupFromFrame()
{
IE ie = new IE();
ie.GoTo("file:///C:/Temp/IFrame.html");
ie.WaitForComplete();
Frame iframe;
try{
iframe = ie.Frame("FrameID");
}
catch (FrameNotFoundException ex){
throw ex;
}
ConfirmDialogHandler confirm = new ConfirmDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, confirm))
{
iframe.Button("myButton1").Click(); // At this point confirm dialog pops up but below lines doesn't work since this is frame instance not ie. is there a way to make it work?
confirm.WaitUntilExists();
confirm.OKButton.Click();
}
}
答案 0 :(得分:2)
更改
iframe.Button("btnId").Click();
到
iframe.Button("myButton1").ClickNoWait();
按钮ID可能只是测试代码中的一个错字,重要的是将Clinck更改为ClickNoWait。我刚刚测试过 - 它适用于我