无法使用selenium单击div中弹出窗口的按钮

时间:2018-04-05 12:47:20

标签: java selenium selenium-webdriver

我正在尝试点击弹出窗口内的按钮。但是,webdriver总是抛出没有这样的元素异常。 弹出窗口不是警报,而是在内部定义的常规元素。 它由一条消息和一个OK按钮组成。我能够验证/找到消息元素但无法单击按钮。 以下是它的html代码。

<div id="yui_patched_v3_11_0_6_1522928024187_16" class="yui3-widget modal yui3-widget-positioned yui3-widget-stacked yui3-widget-modal yui3-resize" style="width: 95%; left: 334px; top: 167px; z-index: 0;" tabindex="0">
    <div id="yui_patched_v3_11_0_6_1522928024187_18" class="modal-content yui3-widget-stdmod">
        <div class="yui3-widget-hd modal-header">
            <div id="yui_patched_v3_11_0_6_1522928024187_112" class="toolbar-content yui3-widget component toolbar">
                <button type="button" class="btn close">×</button>
            </div>
            <h3>Message</h3></div>
        <div class="yui3-widget-bd modal-body">
            <div class="info-block">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tbody>
                        <tr>
                            <td width="127">
                                <div id="info_image_errorPriceConditioNotSelect" class="info-image restriction-image"></div>
                            </td>
                            <td>
                                <div id="info_content_errorPriceConditioNotSelect" class="info-content">None selected</div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="yui3-widget-ft modal-footer">
            <div id="yui_patched_v3_11_0_6_1522928024187_153" class="toolbar-content yui3-widget component toolbar">
                <button type="button" class="btn yui3-widget btn-content btn-focused" id="yui_patched_v3_11_0_6_1522928024187_500">OK</button>
            </div>
        </div>
    </div>
    <div class="yui3-resize-handles-wrapper">
        <div class="yui3-resize-handle yui3-resize-handle-br">
            <div class="yui3-resize-handle-inner yui3-resize-handle-inner-br">&nbsp;</div>
        </div>
    </div>
</div>

以下是我正在尝试访问按钮的代码: -

driver.switchTo().activeElement();
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[starts-with(@id, 'yui_patched_v3_11_')]//div[@id="info_content_errorPriceConditioNotSelect"]"))));
    assertTrue(driver.findElement(By.xpath("//div[starts-with(@id, 'yui_patched_v3_11_')]//div[@id="info_content_errorPriceConditioNotSelect"]")).isDisplayed());
    Thread.sleep(5000);

    driver.findElement(By.xpath("//button[starts-with(@id,"yui_patched_v3_")][text()='OK']")).click(); //Webdriver throws exception here

我正在使用selenium 3.9.1并在chrome上执行脚本 任何帮助将不胜感激。

谢谢,
Anuja

5 个答案:

答案 0 :(得分:2)

即使它没有警报,但在此片刻的活跃元素你会看到它。所以我认为,点击OK可能就是这样:

driver.switchTo().activeElement().submit();

driver.switchTo().activeElement().sendKeys(Keys.ENTER);

答案 1 :(得分:1)

问题在于XPTH。尝试以下代码单击“确定”按钮。

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[text()='OK']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='OK']")));
driver.findElement(By.xpath("//button[text()='OK']")).click();

答案 2 :(得分:0)

使用显式等待直到显示弹出窗口而不是Thread.sleep()。

new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfElementLocated(By.id("yui_patched_v3_11_0_6_1522928024187_500"))).click(); 

答案 3 :(得分:0)

弹出窗口实际上是模态对话框所以点击带有文字的按钮确定你必须诱导 WebDriverWait 如下:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn yui3-widget btn-content btn-focused' and starts-with(@id,'yui_patched_v')]"))).click();

答案 4 :(得分:0)

您的代码不正确

driver.findElement(By.xpath("//button[starts-with(@id,"yui_patched_v3_")][text()='OK']")).click();

注意,您使用"yui_patched_v3_" ID的双引号。这是提前终止xpath。转换为单引号,就像您用于&#39;确定&#39;。

driver.findElement(By.xpath("//button[starts-with(@id,'yui_patched_v3_')][text()='OK']")).click();