如何在Selenium中处理具有“UnexpectedAlertBehaviour”功能的警报?

时间:2012-10-16 11:13:38

标签: java selenium

在selenium framework 2.25中,我看到我们有UnexpectedAlertBehaviour枚举类型,但我不知道如何使用它。

5 个答案:

答案 0 :(得分:9)

我在您的问题上找到了这部分文档: 这也可能对其他人有用:

v2.25.0

=======

的webdriver:

  • 添加了用于处理BASIC和DIGEST身份验证的API

    对话框。目前尚未在任何驱动程序中实现。

  • 警告用户IE驱动程序将不再使用

    中的DLL

    下一个版本。

  • 不推荐使用浏览器的特定WebElement子类。

  • 为远程网络驱动程序添加了对“requiredCapabilities”的支持

    并在firefox中实现了对这些内容的基本支持

    驱动程序。未能满足所需能力将导致

    要抛出的SessionNotCreatedException。

  • 添加了确定未处理警报的方式的功能 处理。这是由“unexpectedAlertBehaviour”处理的 能力,可以是“接受”,“解雇”或 “忽视”。 Java代码应该使用UnexpectedAlertBehaviour 枚举。目前只在Firefox中实现。

  • 允许在Firefox和

    中配置本机事件

    (实验性地)在IE中使用“nativeEvents”功能。

  • 将支持的Firefox版本更新为17。

.....

提供了整个清单here

这是source

package org.openqa.selenium;

    public enum UnexpectedAlertBehaviour {

      ACCEPT ("accept"),
      DISMISS ("dismiss"),
      IGNORE ("ignore")
      ;

      private String text;

      private UnexpectedAlertBehaviour(String text) {
        this.text = text;
      }

      @Override
      public String toString() {
        return String.valueOf(text);
      }

      public static UnexpectedAlertBehaviour fromString(String text) {
        if (text != null) {
          for (UnexpectedAlertBehaviour b : UnexpectedAlertBehaviour.values()) {
            if (text.equalsIgnoreCase(b.text)) {
              return b;
            }
          }
        }
        return null;
      }
    }

我看到你使用unexpectedAlertBehaviour来判断警报是否未处理,如果是,那么你将决定如何处理它。

我想它应该是(我的假设):

try{
alert.accept();
}

catch(org.openqa.selenium.UnexpectedAlertBehaviour){
///...blablabla
}

答案 1 :(得分:2)

它是CapabilityType,因此您必须在创建驱动程序时传递的DesiredCapabilities中表达它。在Python中,我使用以下代码将此行为添加到我的FireFox驱动程序中:

        selenium.webdriver.DesiredCapabilities.FIREFOX["unexpectedAlertBehaviour"] = "accept"

我没有测试过这个Java,但理论上它应该可以工作:

DesiredCapabilities cap = new DesiredCapabailities();
cap.setPreference(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, 
   org.openqa.selenium.UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver = new FirefoxDriver(cap);

答案 2 :(得分:0)

尝试这样做:

DesiredCapabilities ff = DesiredCapabilities.firefox();
ff.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR  UnexpectedAlertBehaviour.ACCEPT);

答案 3 :(得分:0)

要管理您可以获得的任何错误,您只需使用Exception NAMEERROR进行try / block,如下所示:

from selenium.common.exceptions import UnexpectedAlertBehaviour
from selenium.common.exceptions import UnexpectedAlertPresentException
try:
  var = driver.some_function()
  return True
except UnexpectedAlertBehaviour:
  print "We have raised the UnexpectedAlertBehaviour"
  return False
except UnexpectedAlertPresentException:
  print "UnexpectedAlertPresentException"
  return False

我知道这段代码不是Java,但基础是一样的。尝试/捕获例外名称。您可以在我的alert()处理here

的帖子中看到此示例

答案 4 :(得分:0)

如果您在 NodeJS 中使用 Selenium,那么您可以使用 useEffect(() => { const subscribe = { event: "bts:subscribe", data: { channel: `order_book_${currencyPair}` } }; const ws = new WebSocket("wss://ws.bitstamp.net"); const data = new Set(); const flush = () => { for (const value of data) setOrders(JSON.parse(value).data); data.clear(); }; let timer = setInterval(flush, 2000); ws.onopen = () => { ws.send(JSON.stringify(subscribe)); }; ws.onmessage = (event) => { data.add(event.data); }; ws.onclose = () => { ws.close(); }; return () => { clearInterval(timer); ws.close(); flush(); }; }, [currencyPair]);

完整示例(打字稿):

options.setAlertBehavior(UserPromptHandler.ACCEPT)

您无需查找与 DesiredCapabilitiesCapabilities 相关的任何内容,您可以使用 import { Builder } from 'selenium-webdriver'; import { Options } from 'selenium-webdriver/chrome'; import { UserPromptHandler } from 'selenium-webdriver/lib/capabilities'; (async () => { let options = new Options() options.addArguments(`--user-data-dir=./profile`) options.addArguments(`--no-sandbox`) options.windowSize({ width: 1280, height: 720 }) options.setAlertBehavior(UserPromptHandler.ACCEPT) let driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build(); })() 变量填充该值。