Prateek Neelgund
Prateek Neelgund

Reputation: 11

How to write xpath when the button is disabled : Selenium + java + salesforce

I am trying to click on button only if it is enabled, if the button is disabled i would not click on the button and proceed further.

When i write the Xpath and check if the webElement is Enabled, resulting in Enabled always and i dont see much difference in html as well to differential between enabled and disabled.

Help me with writing the xpath which should work only when the element is Enabled.

I have tried to below xpath : //button[NOT(@disable) and span[text()='More Steps']]

Above xpath is not working.

When the element is Enabled :

<button class="slds-button slds-button--neutral showMoreButton uiButton" aria-live="off" type="button" data-aura-rendered-by="2703:0" data-aura-class="uiButton"><!--render facet: 2704:0--><span class=" label bBody" dir="ltr" data-aura-rendered-by="2706:0">More Steps</span><!--render facet: 2708:0--></button>

<span class=" label bBody" dir="ltr" data-aura-rendered-by="2706:0">More Steps</span> 

When the element is Disabled :

<button class="slds-button slds-button--neutral showMoreButton uiButton" aria-live="off" type="button" disabled data-aura-rendered-by="2703:0" data-aura-class="uiButton"><!--render facet: 2704:0--><span class=" label bBody" dir="ltr" data-aura-rendered-by="2706:0">More Steps</span><!--render facet: 2708:0--></button>

<span class=" label bBody" dir="ltr" data-aura-rendered-by="2706:0">More Steps</span> 

Actual : When the element is not disabled, User is able to click on that but when it is disabled still user is trying to click that

Expected : User should be able to click on the element only when it is Enabled

Note : Do not mark the question as duplicate, this is with respect to salesforce tags, please understand.

THanks in advance for your suggestions/Solutions

Upvotes: 0

Views: 14084

Answers (3)

Nikita Sawant
Nikita Sawant

Reputation: 1

"Try out below code-

if(Globals.driver.findElement(By.xpath(""//button[text()='your text']"")).getAttribute(""attribute"").equals(""false"")){ Globals.driver.findElement(By.className(""your button class"")).findElement(By.xpath(""//button[text()='your text']"")).click(); } else{ Globals.driver.findElement(By.className(""your button class"")).findElement(By.xpath(""//button[text()='your text']"")).click();

}"

Upvotes: 0

Denis Markovtsev
Denis Markovtsev

Reputation: 21

When the button is disabled - xpath

//div[span/text()='More Steps']

returns two elements. One of them is in the part of the DOM that is not displayed.

check out the DOM tree

This longer xpath will work as you need because it looks inside active pane only

//div[contains(@class,'active')]//button[not(@disabled) and span/text()='More Steps']

Upvotes: 1

pburgr
pburgr

Reputation: 1778

try this:

public boolean elementIsEnabled = new Boolean(false);

public boolean elementIsEnabled() {
    if (driver.findElements(By.xpath("//button[contains(@data-aura-rendered-by,'2703:0')]")).size() == 1) {
        elementIsEnabled = true;
    }
    else if (driver.findElements(By.xpath("//button[contains(@disabled data-aura-rendered-by,'2703:0')]")).size() == 1) {
        elementIsEnabled = false;
    }
    return elementIsEnabled;
}

Upvotes: 0

Related Questions