Mark Mayo
Mark Mayo

Reputation: 12585

Using Selenium Webdriver to obtain the active status of an element?

We've got a new page we're automating, and some of the features coming through with the bootstrap template are causing some complications for automation.

Traditionally, with a checkbox or radio button, we use a tag to identify it:

[FindsBy(How = How.CssSelector, Using = "body [autotag='prf_rd_1']")]
        private IWebElement myElement;

At which point you can use element.isSelected in Webdriver code to see if it's ticked.

However, with bootstrap devs have changed to use a button which is either active or not active, eg:

<button class="btn" autotag="prf_bt1" btn-radio="availabilityType.key" ng-model="model.availability.time" type="button"> … </button>

<button class="btn **active**" autotag="prf_bt1" btn-radio="availabilityType.key" ng-model="model.availability.time" type="button"> … </button>

(Hmm, not sure how to highly the word active in a code snippet, if anyone knows please feel free to edit)

So, now we can't look for 'isSelected', as it doesn't work like a checkbox.

Does anyone have a code sample for determining whether a button has active class in its css, without using XPATH? Currently we would use the CssSelector as above, and this way elements can move, text can change but as long as that autotag is still there, it's all good. However, I'm unclear how I could do this AND look for other CSS (the class) at the same time to determine if it's active.

Upvotes: 0

Views: 3157

Answers (2)

Arran
Arran

Reputation: 25056

If I understand you correctly, you just want a CSS selector that can locate an element that contains a specific value inside a specific attribute, in this case, it's class.

That's easy, an elaborate way to do this would be:

button[class*='active']

The *= is an attribute wildcard, allowing you to say "where A is somewhere inside that attribute's value".

To specifically look for a button that has a class that contains active and have an auto-tag attribute which is set to prf_bt1:

button[class*='active'][auto-tag='prf_bt1']

Upvotes: 2

Akbar
Akbar

Reputation: 1525

You can use either of these methods to get status of an element,

getAttribute  
getCssValue  
isEnabled

You can get more details on these methods at here.

Upvotes: 0

Related Questions