单击警报内的“确定”按钮(Selenium IDE)

发布于 2024-08-03 01:54:45 字数 220 浏览 4 评论 0原文

我需要使用 Selenium 命令单击警报窗口内的“确定”按钮。我尝试过 assertAlertverifyAlert 但它们没有执行我想要的操作。

可以单击“确定”按钮吗?如果是这样,有人可以给我提供 Selenium IDE 命令的示例吗?

I need to click the 'Ok' button inside an alert window with a Selenium command. I've tried assertAlert or verifyAlert but they don't do what I want.

It's possible the click the 'Ok' button? If so, can someone provide me an example of the Selenium IDE command?

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

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

发布评论

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

评论(14

送君千里 2024-08-10 01:54:45

尝试使用 Selenium 2.0b1。它的核心与第一个版本不同。根据 文档,它应该支持弹出对话框:

弹出对话框

从 Selenium 2.0 开始的 beta 1 中,内置了对处理弹出对话框的支持。触发将打开弹出窗口的操作后,您可以使用以下命令访问警报:

Java

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

Ruby

driver.switch_to.alert

这将返回当前打开的警报对象。有了这个对象,您现在可以接受、关闭、阅读它的内容,甚至可以输入提示。该界面在警报、确认、提示方面同样有效。请参阅 JavaDocs 以获取更多信息。

Try Selenium 2.0b1. It has different core than the first version. It should support popup dialogs according to documentation:

Popup Dialogs

Starting with Selenium 2.0 beta 1, there is built in support for handling popup dialog boxes. After you’ve triggered and action that would open a popup, you can access the alert with the following:

Java

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

Ruby

driver.switch_to.alert

This will return the currently open alert object. With this object you can now accept, dismiss, read it’s contents or even type into a prompt. This interface works equally well on alerts, confirms, prompts. Refer to the JavaDocs for more information.

挖个坑埋了你 2024-08-10 01:54:45

要单击警告框中的“确定”按钮:

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

To click the "ok" button in an alert box:

driver.switchTo().alert().accept();
海夕 2024-08-10 01:54:45

这是 2012 年的一个答案,这个问题是 2009 年的,但人们仍然会看它,并且只有一个正确的(使用 WebDriver)和一个几乎有用(但不够好)的答案。


如果您使用 Selenium RC 并且实际上可以看到警告对话框,那么这是无法完成的。硒应该为你处理它。但是,如 Selenium 文档中所述

Selenium 尝试向您隐藏这些对话框(通过替换
window.alert、window.confirm 和 window.prompt),这样它们就不会停止
执行您的页面。如果您看到弹出警报,则表示
可能是因为它在页面加载过程中触发,即
通常我们保护该页面还为时过早。

这是 Selenium RC(因此也是 Selenium IDE)的一个已知限制,也是开发 Selenium 2 (WebDriver) 的原因之一。如果您想处理 onload JS 警报,您需要 使用 WebDriver 警报处理

也就是说,您可以使用 Robotselenium.keyPressNative() 填写任何文本,然后按 Enter 并盲目确认对话框。这不是最干净的方法,但它可以工作。但是,您将无法收到警报消息。

Robot 将所有有用的键映射到常量,因此这很容易。使用 keyPressNative(),您希望使用 10 作为按 Enter 的值,或使用 27 作为 Esc 的值 因为它适用于 ASCII 代码

This is an answer from 2012, the question if from 2009, but people still look at it and there's only one correct (use WebDriver) and one almost useful (but not good enough) answer.


If you're using Selenium RC and can actually see an alert dialog, then it can't be done. Selenium should handle it for you. But, as stated in Selenium documentation:

Selenium tries to conceal those dialogs from you (by replacing
window.alert, window.confirm and window.prompt) so they won’t stop the
execution of your page. If you’re seeing an alert pop-up, it’s
probably because it fired during the page load process, which is
usually too early for us to protect the page.

It is a known limitation of Selenium RC (and, therefore, Selenium IDE, too) and one of the reasons why Selenium 2 (WebDriver) was developed. If you want to handle onload JS alerts, you need to use WebDriver alert handling.

That said, you can use Robot or selenium.keyPressNative() to fill in any text and press Enter and confirm the dialog blindly. It's not the cleanest way, but it could work. You won't be able to get the alert message, however.

Robot has all the useful keys mapped to constants, so that will be easy. With keyPressNative(), you want to use 10 as value for pressing Enter or 27 for Esc since it works with ASCII codes.

夜雨飘雪 2024-08-10 01:54:45

1|打印警报弹出文本并关闭 -I

Alert alert = driver.switchTo().alert();
System.out.println(closeAlertAndGetItsText());

2|打印警报弹出文本并关闭 -II

Alert alert = driver.switchTo().alert();
System.out.println(alert.getText()); //Print Alert popup
alert.accept(); //Close Alert popup

3|断言警报弹出文本并关闭

Alert alert = driver.switchTo().alert();
assertEquals("Expected Value", closeAlertAndGetItsText());

1| Print Alert popup text and close -I

Alert alert = driver.switchTo().alert();
System.out.println(closeAlertAndGetItsText());

2| Print Alert popup text and close -II

Alert alert = driver.switchTo().alert();
System.out.println(alert.getText()); //Print Alert popup
alert.accept(); //Close Alert popup

3| Assert Alert popup text and close

Alert alert = driver.switchTo().alert();
assertEquals("Expected Value", closeAlertAndGetItsText());
从﹋此江山别 2024-08-10 01:54:45

如果您使用 selenium IDE,则必须手动单击“确定”按钮,因为当运行警报消息命令时浏览器停止工作,如果您想自动单击“确定”按钮,则必须使用 selenium RC 或 webdriver,以下命令适用于 Selenium IDE

在selenium ide中使用storeeval命令,不同类型的盒子

    storeEval | alert("This is alert box")                           |
    storeEval | prompt("This is prompt box. Please enter the value") | text
    storeEval | confirm("this is cofirm box")   |

If you using selenium IDE then you have to click on Ok button manually because when alert message command run that time browser stop working and if you want to click on ok button automatically then you have to use selenium RC or webdriver and below command is for Selenium IDE

In selenium ide use storeeval command, different type of boxes

    storeEval | alert("This is alert box")                           |
    storeEval | prompt("This is prompt box. Please enter the value") | text
    storeEval | confirm("this is cofirm box")   |
那请放手 2024-08-10 01:54:45

您可以查看 chooseOkOnNextConfirmation,尽管如果我正确阅读文档,这可能应该是默认行为。

You might look into chooseOkOnNextConfirmation, although that should probably be the default behavior if I read the docs correctly.

忆依然 2024-08-10 01:54:45

问题不清楚 - 这是页面加载时的警报吗?使用 Selenium 时您不应该看到任何警报对话框,因为它用自己的版本替换了 alert() ,该版本仅捕获为验证而给出的消息。

Selenium 不支持页面加载时的 alert(),因为它需要使用自己的版本来修补测试窗口中的函数。

如果您无法摆脱被测应用程序的加载警报,您应该考虑使用 GUI 自动化来单击生成的弹出窗口,例如 AutoIT(如果您使用的是 Windows)。

The question isn't clear - is this for an alert on page load? You shouldn't see any alert dialogues when using Selenium, as it replaces alert() with its own version which just captures the message given for verification.

Selenium doesn't support alert() on page load, as it needs to patch the function in the window under test with its own version.

If you can't get rid of onload alerts from the application under test, you should look into using GUI automation to click the popups which are generated, e.g. AutoIT if you're on Windows.

韬韬不绝 2024-08-10 01:54:45

使用警报界面,首先 switchTo() 发出警报,然后使用 accept() 单击“确定”或使用 dismiss() 取消它

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

Alert alert_box = driver.switchTo().alert();
alert_box.dismiss(); 

Use the Alert Interface, First switchTo() to alert and then either use accept() to click on OK or use dismiss() to CANCEL it

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

or

Alert alert_box = driver.switchTo().alert();
alert_box.dismiss(); 
隔纱相望 2024-08-10 01:54:45

关于 Selenium IDE,我不是专家,但您必须在触发警报/确认对话框的事件之前添加“在下次确认时选择确定”行,如您在此屏幕截图中所示:

Selenium IDE 订单事项

about Selenium IDE, I am not an expert but you have to add the line "choose ok on next confirmation" before the event which trigger the alert/confirm dialog box as you can see into this screenshot:

Selenium IDE order matter

霓裳挽歌倾城醉 2024-08-10 01:54:45

assertAlert 应该可以解决问题。我在文档中看到,在页面的 OnLoad 事件处理程序中生成的警报不能以这种方式编写脚本(唉,由于 ASP.NET 页面生命周期,我自己也经历过这种情况)。这可能就是你遇到的情况吗?

assertAlert ought to do the trick. I see in the docs that alerts generated in a page's OnLoad event handler cannot be scripted this way (and have experienced it myself, alas, due to the ASP.NET page lifecycle). Could that be what you're running into?

柠檬 2024-08-10 01:54:45

对于 selenium,警报是使用 javascript 引发的警报,例如,

 javascript:alert();

有一项基本检查来验证您的警报是否实际上是 javascript 警报,或者只是一个用于显示某些消息的基于 div 的框。
如果它是 javascript 警报,则在运行 selenium 脚本时您将无法在屏幕上看到它。

如果您能够看到它,那么您需要获取警报的“确定”按钮的定位器并使用 selenium.click(locator) 来关闭警报。如果您可以提供更多上下文,可以更好地帮助您:

  1. IDE 还是 RC?
  2. 警报 Selenium 脚本的 HTML 代码

瓦米普

For selenium, an alert is the one which raised using javascript e.g.

 javascript:alert();

There is one basic check to verify whether your alert is actually a javascript alert or just a div-based box for displaying some message.
If its a javascript alert, you wont be able to see it on screen while running the selenium script.

If you are able to see it, then you need to get the locator of the ok button of the alert and use selenium.click(locator) to dismiss the alert. Can help you better if you can provide more context:

  1. IDE or RC?
  2. HTML code of the alert
  3. your selenium script.

Vamyip

野生奥特曼 2024-08-10 01:54:45

使用 ChooseOkOnNextConfirmation() 消除警报,并使用 getAlert() 验证警报是否已显示(并可选择抓取其文本进行验证)。

selenium.chooseOkOnNextConfirmation();  // prepares Selenium to handle next alert
selenium.click(locator);
String alertText = selenium.getAlert(); // verifies that alert was shown
assertEquals("This is a popup window", alertText);
...

Use chooseOkOnNextConfirmation() to dismiss the alert and getAlert() to verify that it has been shown (and optionally grab its text for verification).

selenium.chooseOkOnNextConfirmation();  // prepares Selenium to handle next alert
selenium.click(locator);
String alertText = selenium.getAlert(); // verifies that alert was shown
assertEquals("This is a popup window", alertText);
...
可遇━不可求 2024-08-10 01:54:45

这是Python代码
警报框的问题(特别是甜蜜警报是它们有一个
延迟,而 Selenium 太快了)

对我有用的一个选项是:

while True:
    try:
        driver.find_element_by_xpath('//div[@class="sweet-alert showSweetAlert visible"]')
        break
    except:
        wait = WebDriverWait(driver, 1000)

confirm_button = driver.find_element_by_xpath('//button[@class="confirm"]')
confirm_button.click()

This is Pythoncode
Problem with alert boxes (especially sweet-alerts is that they have a
delay and Selenium is pretty much too fast)

An Option that worked for me is:

while True:
    try:
        driver.find_element_by_xpath('//div[@class="sweet-alert showSweetAlert visible"]')
        break
    except:
        wait = WebDriverWait(driver, 1000)

confirm_button = driver.find_element_by_xpath('//button[@class="confirm"]')
confirm_button.click()
想你的星星会说话 2024-08-10 01:54:45

新的 Selenium IDE(2019 年发布)拥有更广泛的 API 和新文档。

我相信这是您想要尝试的命令:

webdriver在可见确认上选择确定

描述于:

https://www.seleniumhq.org/selenium-ide/docs/en/api/commands/#webdriver -choose-ok-on-visible-confirmation

还有其他与警报相关的 API 调用;只需在该页面搜索警报

The new Selenium IDE (released in 2019) has a much broader API and new documentation.

I believe this is the command you'll want to try:

webdriver choose ok on visible confirmation

Described at:

https://www.seleniumhq.org/selenium-ide/docs/en/api/commands/#webdriver-choose-ok-on-visible-confirmation

There are other alert-related API calls; just search that page for alert

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