无法使用Selenium单击Div中弹出窗口的按钮



我正在尝试单击弹出窗口中的按钮。但是,WebDriver始终不会抛出此类元素异常。弹出窗口不是警报,而是在a内定义的常规元素。它由消息和确定按钮组成。我可以验证/找到消息元素,但无法单击该按钮。以下是它的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

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

谢谢,
anuja

问题是xpath。尝试以下代码以获取按钮单击。

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();

即使它不是警报,但此刻的活动元素也可以看到。所以我认为,单击确定可能是可以的:

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

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

使用明确等待等待弹出弹出而不是thread.sleep((。

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

pop-up 实际上是模态对话框,请单击带有文本的按钮,如 ok ok 您必须诱导 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();

您的代码不正确

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

注意,您正在使用"yui_patched_v3_" ID的双引号。这正在尽早终止XPATH。转换为" ok''一样的引号。

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

最新更新