简体   繁体   English

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

[英]Unable to click on button of a pop-up in div using selenium

I'm trying to click on a button inside pop-up.我正在尝试单击弹出窗口中的按钮。 However, webdriver always throws No such element exception.但是,webdriver 总是抛出 No such element 异常。 The pop-up is not an alert but a regular element defined inside a .弹出窗口不是警报,而是定义在 . It consists of a message and an OK button.它由一条消息和一个确定按钮组成。 I'm able to verify / locate the message element but unable to click on the button.我能够验证/定位消息元素,但无法单击按钮。 Following is the html code for it.以下是它的 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>

Below is my code through which I'm trying to access the button:-以下是我尝试访问按钮的代码:-

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

I'm using selenium 3.9.1 and executing the scripts on chrome.我正在使用 selenium 3.9.1 并在 chrome 上执行脚本。
Any help would be appreciated.任何帮助将不胜感激。

Thanks,谢谢,
Anuja阿努贾

Issue is with XPATH.问题在于 XPATH。 Try below code for OK button click.尝试下面的代码单击确定按钮。

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

Even if it is no alert but the active element at this Moment you see it.即使它不是警报而是此时此刻的活动元素,您也会看到它。 So I think, clicking on OK might be possible in this way:所以我认为,通过这种方式点击 OK 可能是可能的:

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

or

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

弹出窗口实际上是一个模态对话框,因此单击文本为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();

Your code is incorrect您的代码不正确

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

Note, you are using double quotes for the "yui_patched_v3_" ID.请注意,您对"yui_patched_v3_" ID 使用了双引号。 This is terminating the xpath early.这是提前终止 xpath。 Convert to single quotes like you used for 'OK'.像用于 'OK' 一样转换为单引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM