简体   繁体   中英

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. 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.

<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.
Any help would be appreciated.

Thanks,
Anuja

Issue is with 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:

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. This is terminating the xpath early. Convert to single quotes like you used for 'OK'.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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