<small id='DPv2D'></small><noframes id='DPv2D'>

  • <i id='DPv2D'><tr id='DPv2D'><dt id='DPv2D'><q id='DPv2D'><span id='DPv2D'><b id='DPv2D'><form id='DPv2D'><ins id='DPv2D'></ins><ul id='DPv2D'></ul><sub id='DPv2D'></sub></form><legend id='DPv2D'></legend><bdo id='DPv2D'><pre id='DPv2D'><center id='DPv2D'></center></pre></bdo></b><th id='DPv2D'></th></span></q></dt></tr></i><div id='DPv2D'><tfoot id='DPv2D'></tfoot><dl id='DPv2D'><fieldset id='DPv2D'></fieldset></dl></div>
    <legend id='DPv2D'><style id='DPv2D'><dir id='DPv2D'><q id='DPv2D'></q></dir></style></legend>
    <tfoot id='DPv2D'></tfoot>
    • <bdo id='DPv2D'></bdo><ul id='DPv2D'></ul>

      1. Selenium 自动接受警报

        时间:2023-07-13

        <legend id='wx0GT'><style id='wx0GT'><dir id='wx0GT'><q id='wx0GT'></q></dir></style></legend>

            <tbody id='wx0GT'></tbody>

              <i id='wx0GT'><tr id='wx0GT'><dt id='wx0GT'><q id='wx0GT'><span id='wx0GT'><b id='wx0GT'><form id='wx0GT'><ins id='wx0GT'></ins><ul id='wx0GT'></ul><sub id='wx0GT'></sub></form><legend id='wx0GT'></legend><bdo id='wx0GT'><pre id='wx0GT'><center id='wx0GT'></center></pre></bdo></b><th id='wx0GT'></th></span></q></dt></tr></i><div id='wx0GT'><tfoot id='wx0GT'></tfoot><dl id='wx0GT'><fieldset id='wx0GT'></fieldset></dl></div>

                <small id='wx0GT'></small><noframes id='wx0GT'>

                <tfoot id='wx0GT'></tfoot>
                • <bdo id='wx0GT'></bdo><ul id='wx0GT'></ul>
                  本文介绍了Selenium 自动接受警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有谁知道如何禁用它?或者如何从已自动接受的警报中获取文本?

                  Does anyone know how to disable this? Or how to get the text from alerts that have been automatically accepted?

                  这段代码需要工作,

                  driver.findElement(By.xpath("//button[text() = "Edit"]")).click();//causes page to alert() something
                  Alert alert = driver.switchTo().alert();
                  alert.accept();
                  return alert.getText();
                  

                  但是却给出了这个错误

                  No alert is present (WARNING: The server did not provide any stacktrace information)
                  Command duration or timeout: 2.14 seconds
                  

                  我正在使用带有 Selenium 2.32 的 FF 20

                  I am using FF 20 with Selenium 2.32

                  推荐答案

                  就在前几天我回答了类似的问题,所以它仍然很新鲜.您的代码失败的原因是,如果在处理代码时未显示警报,它将大部分失败.

                  Just the other day i've answered something similar to this so it's still fresh. The reason your code is failing is if the alert is not shown by the time the code is processed it will mostly fail.

                  谢天谢地,来自 Selenium WebDriver 的人已经为它实现了等待.因为你的代码就这么简单:

                  Thankfully, the guys from Selenium WebDriver have a wait already implemented for it. For your code is as simple as doing this:

                  String alertText = "";
                  WebDriverWait wait = new WebDriverWait(driver, 5);
                  // This will wait for a maximum of 5 seconds, everytime wait is used
                  
                  driver.findElement(By.xpath("//button[text() = "Edit"]")).click();//causes page to alert() something
                  
                  wait.until(ExpectedConditions.alertIsPresent());
                  // Before you try to switch to the so given alert, he needs to be present.
                  
                  Alert alert = driver.switchTo().alert();
                  alertText = alert.getText();
                  alert.accept();
                  
                  return alertText;
                  

                  您可以从 ExpectedConditions 这里,如果你想要这个方法背后的代码这里.

                  You can find all the API from ExpectedConditions here, and if you want the code behind this method here.

                  这段代码也解决了这个问题,因为关闭警报后无法返回alert.getText(),所以我为你存储在一个变量中.

                  This code also solves the problem because you can't return alert.getText() after closing the alert, so i store in a variable for you.

                  这篇关于Selenium 自动接受警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 C# 中使用 WebDriver 获取指定元素的屏幕截图 下一篇:Selenium 等待任何元素可见

                  相关文章

                  <small id='41dpv'></small><noframes id='41dpv'>

                  <tfoot id='41dpv'></tfoot>
                • <i id='41dpv'><tr id='41dpv'><dt id='41dpv'><q id='41dpv'><span id='41dpv'><b id='41dpv'><form id='41dpv'><ins id='41dpv'></ins><ul id='41dpv'></ul><sub id='41dpv'></sub></form><legend id='41dpv'></legend><bdo id='41dpv'><pre id='41dpv'><center id='41dpv'></center></pre></bdo></b><th id='41dpv'></th></span></q></dt></tr></i><div id='41dpv'><tfoot id='41dpv'></tfoot><dl id='41dpv'><fieldset id='41dpv'></fieldset></dl></div>

                  <legend id='41dpv'><style id='41dpv'><dir id='41dpv'><q id='41dpv'></q></dir></style></legend>

                      • <bdo id='41dpv'></bdo><ul id='41dpv'></ul>