无法查看对话框并使用selenium webdriver单击“确定”

时间:2014-11-01 04:42:35

标签: selenium dialog

我需要使用Selenium webdriver处理对话框处理的帮助。这是firebug中的html for对话框

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all xx_form_dialog ui-draggable ui-resizable ui-dialog-buttons"
    style="outline: 0px none; z-index: 20012; position: absolute; height: auto; width: 300px; top: 166.997px; left: 734px; display: block;"
    tabindex="-1" role="dialog" aria-labelledby="ui-id-27">
    <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
        <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
            <div class="ui-dialog-buttonset">
                <button id="information-ok" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
                    <span class="ui-button-text">OK</span>
                </button>
            </div>
       </div>
    </div>
<div>   

这就是我的尝试:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[17]"))).click();
driver.findElement(By.id("information-ok")).click();

1 个答案:

答案 0 :(得分:0)

使用以下代码解决了问题。创建了一个对话框处理方法。在博客中搜索时找到答案。参考号17799440.感谢Sriram的回答:

public void aLert()
{
    boolean a = false;
    try {
        Alert alert = driver.switchTo().alert();
        String AlertText = alert.getText();
        System.out.println(AlertText);
        alert.accept();
        a = true;
    } catch (Exception e) {
        a = false;
    }finally {
        if (a != true) {
            // take ur screenshot or whatever
            driver.findElement(By.id("information-ok")).click();
        }
    }

}