ceddyyy
ceddyyy

Reputation: 27

Selenium: Button in modal content is not clickable

I want to Click the "OK" button during a Selenium test but the Element is not visible.

 driver.findElement(By.xpath("//*[@id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]")).click();
<div class="bootstrap-dialog-footer-buttons">
    <button class="btn btn-default" id="5a4bb849-7a61-4603-9ef2-f9e0ecab4523">
        <span class="glyphicon glyphicon-ban-circle"></span> Cancel
    </button>
    <button class="btn btn-warning" id="f7f4b18b-2ba2-4c1e-b541-a254c080f398">
        <span class="glyphicon glyphicon-ok"></span> Ok
    </button>
</div>

Upvotes: 0

Views: 4210

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193088

As per the HTML you have shared it seems that the desired element is within a Bootstrap Modal Dialog Box and the id attribute of the element is dynamic. So to invoke click() you have to induce WebDriverWait as follows :

  • cssSelector :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.bootstrap-dialog-footer-buttons button.btn.btn-warning"))).click();
    
  • xpath :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='bootstrap-dialog-footer-buttons']//button[@class='btn btn-warning']"))).click();
    

Upvotes: 0

Amit Gurbani
Amit Gurbani

Reputation: 607

Use JavascriptExecutor to click the element,

Refer code,

WebElement element = driver.findElement(By.xpath("//*[@id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Upvotes: 1

BhavinD.
BhavinD.

Reputation: 481

I think in your DOM, the button id is changing dynamically. Whenever page reload it will generating new id. There is different button id you used in your Selenium code and HTML. So, I suggest you go with className. Try below code and hope it works for you.

        //If the Element is not visible then wait until that element is not visible
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.className("btn btn-warning")));

        //If the element is visible but not Clickable then wait until that element get Clickable.       
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.className("btn btn-warning")));

        //Then simply click the button
        driver.findElement(By.className("btn btn-warning")).click();

Upvotes: 2

Related Questions