Selenium WebDriver 如何关闭浏览器弹出窗口

发布于 2024-11-26 17:06:11 字数 149 浏览 0 评论 0原文

我正在使用 selenium webDriver 为 Web 应用程序编写测试,并遇到了一个场景,当我尝试关闭浏览器时,我会收到一个弹出窗口,显示“您确定吗?该页面要求您确认是否要离开 - 输入的数据将迷路了。”有 2 个按钮:离开页面和留在页面

我如何单击这些按钮?

I am writing tests for a web App using selenium webDriver and came across a scenario where when I try to close the browser I get a popup saying "Are you sure? The page is asking you to confirm that you want to leave - data entered will be lost." with 2 buttons: Leave Page and Stay on Page

How do I click on those buttons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

强者自强 2024-12-03 17:06:11
 ( ( JavascriptExecutor ) _driver )
            .executeScript( "window.onbeforeunload = function(e){};" );

为我解决了这个问题

 ( ( JavascriptExecutor ) _driver )
            .executeScript( "window.onbeforeunload = function(e){};" );

solved the issue for me

一向肩并 2024-12-03 17:06:11
IAlert alert = driver.SwitchTo().Alert();

alert.Accept(); //for two buttons, choose the affirmative one
// or
alert.Dismiss(); //to cancel the affirmative decision, i.e., pop up will be dismissed and no action will take place
IAlert alert = driver.SwitchTo().Alert();

alert.Accept(); //for two buttons, choose the affirmative one
// or
alert.Dismiss(); //to cancel the affirmative decision, i.e., pop up will be dismissed and no action will take place
再可℃爱ぅ一点好了 2024-12-03 17:06:11

您可以使用 警报API。切换到警报并确认它看起来像这样(在 Java 中):

Alert alert = driver.switchTo().alert();
alert.accept();

Selenium 常见问题解答

You can interact with alerts and the like using the Alert API. Switching to the alert and confirm it would look like this (in Java):

Alert alert = driver.switchTo().alert();
alert.accept();

This is also explained in the Selenium FAQ.

一瞬间的火花 2024-12-03 17:06:11
def close_all_popups(driver):
    driver.window_handles
    for h in driver.window_handles[1:]:
        driver.switch_to_window(h)
        driver.close()
    driver.switch_to_window(driver.window_handles[0])
def close_all_popups(driver):
    driver.window_handles
    for h in driver.window_handles[1:]:
        driver.switch_to_window(h)
        driver.close()
    driver.switch_to_window(driver.window_handles[0])
夕嗳→ 2024-12-03 17:06:11

您可以尝试使用键盘事件。因此,一旦窗口弹出:

点击“确定”按钮。

driver.Keyboard.PressKey(Keys.Tab);

您必须尝试看看需要按多少次选项卡。

然后击中空间。

driver.Keyboard.PressKey(Keys.Space);

是的,这有点像黑客,但 WebDriver 仍然相当不成熟。

编辑:这适用于“真正的”弹出窗口,但正如另一位回答者所说,可能不适用于奇怪的页面内内容。基本上,如果您可以手动切换到关闭按钮,则此方法应该有效。

You might try using keyboard events. So once the window pops up:

Tab onto the "Ok" button.

driver.Keyboard.PressKey(Keys.Tab);

You'll have to play around to see how many tab presses are required.

Then hit space.

driver.Keyboard.PressKey(Keys.Space);

Yeah it's sort of a hack, but WebDriver is still pretty immature.

EDIT: This will work for "real" popups, but as another answerer said, maybe not for weird in-page things. Basically, if you can tab to the close button manually, this method should work.

做个ˇ局外人 2024-12-03 17:06:11

不确定您使用的弹出窗口的结构是什么。
如果您使用过 as 弹出窗口,那么这里有一些可能对您有用。

如果是警报的话。您可以尝试:

driver.SwitchTo().Alert()

如果弹出一个窗口,则:

driver.SwitchTo().Window(<windowname>)

对于 iFrame:

driver.SwitchTo().Frame("iFrmLinks")

一旦完成这三个窗口中的任何一个,您就可以访问其所有内部元素。

问候
塔希尔

Not sure what is the structure of the popup that you have used.
Here are a few that may work for you if you have used either of as to popup.

If its an alert. you can try:

driver.SwitchTo().Alert()

If its a window that pops up then:

driver.SwitchTo().Window(<windowname>)

For iFrame:

driver.SwitchTo().Frame("iFrmLinks")

Once you get through either of the three then you can access all its internal elements.

Regards
Tahir

梦年海沫深 2024-12-03 17:06:11

当我有字段形式和“完成编辑”提交按钮时,我遇到了同样的问题,因为当 Selenium IDE 自动单击 javascript 函数时,负责禁用确认窗口(离开页面或仍在页面上),它确实不采取“mouseup”鼠标事件意味着 window.confirm 仍然有效并且自动通过测试失败。在这种情况下,我的解决方案是覆盖 javascript 函数 window.onbeforeunload (无需询问我们是否知道在 Selenium IDE 中记录测试时我们会这样做)。您可以在 Selnium IDE 中运行覆盖脚本,然后通过 selenium.runScript 单击“保存”(或类似的内容)按钮 - 它应该简单地禁用确认窗口。

    Command: runScript
    Target: {window.onbeforeunload=function(e){};}

I've got the same problem when i have the form of fields and "done editing" submit button, because when Selenium IDE auto-click the javascript function, that responsible to disable confirmation window (leave page or still on it), it does not take "mouseup" mouse event that mean window.confirm still works and auto-pass test was fails. My solution is override javascript function window.onbeforeunload in this case (no need to ask if we know that we do when we record test in Selenium IDE). You can run override script in the Selnium IDE before click on "Save" (or something like this) button through selenium.runScript - it should simple disable the confirmation window.

    Command: runScript
    Target: {window.onbeforeunload=function(e){};}
半山落雨半山空 2024-12-03 17:06:11

您需要使用 try catch 块来处理意外警报。将代码放入 try 块并捕获“UnhandledAlertException”

示例:

try{
     .....
     .....
     code here
}catch(UnhandledAlertException e ){
    driver.switchto().alert().dismiss();
}

You need to handle the unexpected alerts using try catch blocks. Put your code in try block and catch the 'UnhandledAlertException'

Example:

try{
     .....
     .....
     code here
}catch(UnhandledAlertException e ){
    driver.switchto().alert().dismiss();
}
简单气质女生网名 2024-12-03 17:06:11

这里禁用所有弹出窗口

    Dictionary<string, object> commandParameters = new Dictionary<string, object>
    {
        ["source"] = @"
window.alert = function() {}; 
window.confirm = function() { return true; }; 
window.prompt = function(message, defaultValue) { return null; };
window.onbeforeunload = function(){}; 
window.setInterval(function(){window.onbeforeunload = function(){};},1);

let func_addEventListener = window.addEventListener; 
window.addEventListener = function(type,listener,options){ if(type != 'beforeunload') func_addEventListener(type,listener,options);}"
    };
    chromeDriver.ExecuteCdpCommand("Page.addScriptToEvaluateOnNewDocument", commandParameters);

setInterval 可能会延迟一点,请确保在离开页面之前延迟一点

Here disable all popup

    Dictionary<string, object> commandParameters = new Dictionary<string, object>
    {
        ["source"] = @"
window.alert = function() {}; 
window.confirm = function() { return true; }; 
window.prompt = function(message, defaultValue) { return null; };
window.onbeforeunload = function(){}; 
window.setInterval(function(){window.onbeforeunload = function(){};},1);

let func_addEventListener = window.addEventListener; 
window.addEventListener = function(type,listener,options){ if(type != 'beforeunload') func_addEventListener(type,listener,options);}"
    };
    chromeDriver.ExecuteCdpCommand("Page.addScriptToEvaluateOnNewDocument", commandParameters);

That setInterval may be delay a bit, make sure you delay a bit before leave page

黑白记忆 2024-12-03 17:06:11

将其中之一放在点击事件之前:

selenium.chooseCancelOnNextConfirmation()

selenium.chooseOkOnNextConfirmation()

Put one of these before the click event:

selenium.chooseCancelOnNextConfirmation()

selenium.chooseOkOnNextConfirmation()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文