Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to access bootstrap modal dialog in Selenium Webdriver

I want to Access the content of the model dialog box open, and want to access buttons (Yes,No).

Here is HTML code looks like

<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header"><div class="bootstrap-dialog-header">
        <div class="bootstrap-dialog-close-button" style="display: none;">
            <button class="close">×</button>
        </div>
        <div class="bootstrap-dialog-title" id="e6adf6aa-dcbf-4fb8-9935-c083762f2812_title">
        Inactivate user
        </div>
    </div>
</div>
<div class="modal-body">
    <div class="bootstrap-dialog-body">
        <div class="bootstrap-dialog-message">
        Are you sure you want Inactivate user?
        </div>
    </div>
</div>
<div class="modal-footer">
    <div class="bootstrap-dialog-footer">
        <div class="bootstrap-dialog-footer-buttons">
            <button class="btn btn-default" id="868d2d8a-67f6-4308-a3c8-0246a5d4618c">Yes</button>
            <button class="btn btn-default" id="23e4d027-ef32-4b58-b8b6-6f95bead2db4">No</button>
        </div>
    </div>
</div>

One more thing is id of the buttons are dynamic.

like image 872
Nikunj Avatar asked Sep 30 '15 08:09

Nikunj


People also ask

How do I trigger a bootstrap modal?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do you show modal dialog?

The Window. showModalDialog() created and displayed a modal dialog box containing a specified HTML document.

How do I show modal pop on page load?

Answer: Use the Bootstrap . modal('show') method modal('show') method for launching the modal window automatically when page load without clicking anything. A common example of this technique is loading the modal when user landed on the home page and requesting them to subscribe the website newsletter.


2 Answers

I found the solution for this after so much googling... here is the simple solution for this

//Switch to active element here in our case its model dialogue box.
driver.switchTo().activeElement();

Thread.sleep(3000);

// find the button which contains text "Yes" as we have dynamic id
driver.findElement(By.xpath("//button[contains(text(),'Yes')]")).click();

This code solves my problem.

like image 102
Nikunj Avatar answered Sep 23 '22 02:09

Nikunj


Instead ID try using xpath like:

By.XPath("//div[@class='bootstrap-dialog-footer-buttons']//button[text()='Yes']")

I hope that helps.

like image 44
Boris Stoyanov Avatar answered Sep 23 '22 02:09

Boris Stoyanov