English 中文(简体)
位于 Java的Selenium网络司机(ium2)的警报处理
原标题:Alert handling in Selenium WebDriver (selenium 2) with Java

我想查明警报是否普及。 目前,我使用以下法典:

    try {
        Alert alert = webDriver.switchTo().alert();

        // check if alert exists
        // TODO find better way
        alert.getText();

        // alert handling
        log().info("Alert detected: {}" + alert.getText());
        alert.accept();
    } catch (Exception e) {
    }

问题是,如果网页的目前状况没有警惕,它就等待一定时间,直到时间的到来,那么就会出现一个例外,因此情况确实很差。

是否有更好的办法,或许是我可以用来发出动态警报的警事手吗?

最佳回答
问题回答

撰写以下方法:

public boolean isAlertPresent() {
    try {
        driver.switchTo().alert();
        return true;
    } // try
    catch (Exception e) {
        return false;
    } // catch
}

现在,你可以通过使用上述方法,检查是否有警报:

if (isAlertPresent()) {
    driver.switchTo().alert();
    driver.switchTo().alert().accept();
    driver.switchTo().defaultContent();
}
Alert alert = driver.switchTo().alert();

alert.accept();

你们也可以减少警示箱:


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

alert().dismiss();
try 
    {
        //Handle the alert pop-up using seithTO alert statement
        Alert alert = driver.switchTo().alert();

        //Print alert is present
        System.out.println("Alert is present");

        //get the message which is present on pop-up
        String message = alert.getText();

        //print the pop-up message
        System.out.println(message);

        alert.sendKeys("");
        //Click on OK button on pop-up
        alert.accept();
    } 
    catch (NoAlertPresentException e) 
    {
        //if alert is not present print message
        System.out.println("alert is not present");
    }

你可以尝试

 try{
        if(webDriver.switchTo().alert() != null){
           Alert alert = webDriver.switchTo().alert();
           alert.getText();
           //etc.
        }
    }catch(Exception e){}

如果这种工作不成功,你可以尝试通过所有窗口处理,看看是否有警报。 我不敢确定警报是否打开使用 se的新窗口。

for(String s: webDriver.getWindowHandles()){
 //see if alert exists here.
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签