selenium-webdriverjquery-confirm

Getting ok button to close jquery-confirm window using Selenium


I am completely new to Selenium as of today and i am having trouble getting a button to be clicked.

I have the following code for a jquery-confirm window that is displayed when my webpage is first displayed:

 $(document).ready(function() {
        $.confirm({
            title: 'LF Demo',
            content: 'This is a demo etc',
            type: 'orange',
            typeAnimated: true,
            buttons: {
                ok: function () {
                }
            }
        });
    });

On inspect it looks like this:

<div class="jconfirm-box jconfirm-hilight-shake jconfirm-type-orange jconfirm-type-animated" role="dialog" aria-labelledby="jconfirm-box73605" tabindex="-1" style="transition-duration: 0.4s; transition-timing-function: cubic-bezier(0.36, 0.55, 0.19, 1); transition-property: all, margin;"><div class="jconfirm-closeIcon" style="display: none;">×</div><div class="jconfirm-title-c"><span class="jconfirm-icon-c"></span><span class="jconfirm-title">LF Demo</span></div><div class="jconfirm-content-pane no-scroll" style="transition-duration: 0.4s; transition-timing-function: cubic-bezier(0.36, 0.55, 0.19, 1); max-height: 640.312px;"><div class="jconfirm-content" id="jconfirm-box73605"><div>This is a demo etc</div></div></div><div class="jconfirm-buttons"><button type="button" class="btn btn-default">ok</button></div><div class="jconfirm-clear"></div></div>

I am trying to use Selenium to select the ok button on the window and close the window.

I can get the test to run with these lines:

wait = new WebDriverWait(driver, new System.TimeSpan(0, 0, 4));
    IWebElement window = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.CssSelector(".jconfirm-box")));

But despite much Googling I cannot work out what line is required to get the ok button on the window to be activated and the jquery-confirm window to close.

lines like this:

window.FindElement(By.Id("ok")).Click();

give me this error:

 OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"css selector","selector":".ok"}

Thank you.


Solution

  • window.FindElement(By.Id("ok")).Click();
    

    In the shared HTML, I don't see any element which has ID attributes value as ok. This is the reason your code is throwing NoSuchElementException

    Use XPath locator strategy to locate the OK button. See code below:

    window.FindElement(By.XPath("//button[text()='ok']")).Click();