user2357236
user2357236

Reputation: 33

Java/SeleniumWebDriver - Click a JQuery dialog button with Selenium

I currently trying to test a dialog with Selenium WebDriver but the "Create Employee" button doesn't have an id nor a name. This is what the code looks like.

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
    <div class="ui-dialog-buttonset">

         <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
              <span class="ui-button-text">Create Employee</span>
         </button>

         <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
              <span class="ui-button-text">Cancel</span>
         </button>

    </div>

         <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
              <span class="ui-button-text">Create Employee</span>
         </button>

         <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
              <span class="ui-button-text">Cancel</span>
         </button>

</div>

Can anyone provide me with the java code that will click the button in the dialog box? Thanks

Upvotes: 1

Views: 4775

Answers (4)

mcr
mcr

Reputation: 4705

As an alternative to the xpath stuff, which I found troublesome for newbies, I found that jQueryUI dialog can add IDs to the results.

Instead of:

$("#stuff").dialog({
    buttons: {
            "Yes": function() {

write:

$("#stuff").dialog({
    buttons: {
            "Yes": { text: "Yes",
                     id: "#mystuff-yes",
                     click: function() {

Upvotes: 1

Omkar
Omkar

Reputation: 182

Try Below code.

 driver.findelement(By.xpath("//button[@type='button']/span[text()='Create Employee']")).click();

Upvotes: 4

me_digvijay
me_digvijay

Reputation: 5502

If you don't have any way to select any element I would suggest the use of xpath I would suggest installing some addons or extensions on your browser for getting the xpath of the element you want to interact. Then Use that xpath for your purpose.

Upvotes: 0

Eli
Eli

Reputation: 14827

If your text does not change, you can use:

$('.ui-button-text').click(function() {
    if($(this).text() == 'Create Employee') {
        // Your code here
    }
});

Demo

Upvotes: -1

Related Questions