4

I have the following angular.js on a page. The items being displayed are angular.js Li items. One is greyed out the other is enabled. When I use the Selenium webdriver method .isEnabled(), both the greyed out and enabled items return "enabled".

The first question is how do I get .isEnabled() to work with this type of element? Q The second question is, assuming webdriver won't do it and I need to xpath, I guess I could use something like this:

$x("//li[@class ='ng-scope disabled' and @id='actionCUSTARD']")
$x("//li[@class ='ng-scope' and @id='actionRHUBARB']")

The first returns something only if the given id is disabled, the second only if the given Id is enabled, this could be built into a Java method to check that for a given id the element is enabled or disabled. Is there an easier way of doing this?

</li>
<li id="actionRHUBARB" class="ng-scope" on="deriveInvokeType(action)" ng-switch="" ng-class="{'disabled': false}" ng-repeat="action in getActionList()">
    <!--
     ngSwitchWhen: LINK_DYNAMIC
    -->
    <!--
     ngSwitchWhen: NO_INVOKE_PERMISSION
    -->
    <!--
     ngSwitchDefault: 
    -->
    <a class="ng-scope ng-binding" ng-click="doAction(action)" ng-switch-default="" href=""></a>
</li>
<li id="actionCUSTARD" class="ng-scope disabled" on="deriveInvokeType(action)" ng-switch="" ng-class="{'disabled': true}" ng-repeat="action in getActionList()">
    <!--
     ngSwitchWhen: LINK_DYNAMIC
    -->
    <!--
     ngSwitchWhen: NO_INVOKE_PERMISSION
    -->
    <!--
     ngSwitchDefault: 
    -->
    <a class="ng-scope ng-binding" ng-click="doAction(action)" ng-switch-default="" href=""></a>
</li>   

2 Answers 2

2

The element is probably disabled with the style pointer-events set to none which is not considered by .isEnabled(). If it's the case you could evaluate the CSS value returned by .getCssValue:

boolean isEnabled = !"none".equals(element.getCssValue("pointer-events"));

Or with a piece of JavaScript:

boolean isEnabled = (boolean)((JavascriptExecutor)driver).executeScript(
    "return window.getComputedStyle(arguments[0], null).getPropertyValue('pointer-events') === 'none'"
    , element);

You could also determine the state by checking the presence of the class disable, but it won't guarantee that the element is disabled by the implemented style.

Sign up to request clarification or add additional context in comments.

Comments

0

According to Your first question you can simply get element using By.id and check isEnabled() as below :-

WebElement el = driver.findElement(By.id("actionRHUBARB"))

or

WebElement el = driver.findElement(By.id("actionCUSTARD"))

and check for enabled as :-

if(el.isEnabled()) {
   //do your stuff
}

Note :- findElement always gives you element if it is present on the DOM otherwise it will throws NoSuchElementException

Now according to your second question if selenium failed to find enabled element, you can use By.cssSelector as below :-

WebElement el = driver.findElement(By.cssSelector(":not(.disabled)[id = 'actionRHUBARB']"));
//it will find enabled element with id actionRHUBARB

WebElement el = driver.findElement(By.cssSelector(":not(.disabled)[id = 'actionCUSTARD']"));
//it will throw NoSuchElementException because it is not enabled

Hope it helps...:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.