0

I have trying to get the number of buttons(count) enabled in a web page using their class names. But I couldn't get it as I wanted. Always I get count as 0.

    int count = 0;
    try {
        List<WebElement> Elements = driver.findElements(By.className(Locators.Buttons));
        for (int Counter = 0; Counter < Elements.size(); Counter++) {
            count = Counter;
        }
        System.out.print("############################################# " + count);

    }
    catch (Exception e)
    {

    }
    return count;

I have declared xpath as follows

//button[class='btn btn--05 thm--01']       

Other buttons have a class names starting like this. I tried using contains keyword. But didn't work.

Number of buttons enabled can be varied. Therefore buttons should be identified using above class name. How can I do it?
I have tried doing these examples too.
Similar question asked

2 Answers 2

2

@ is missing from the Xpath you have provided:

It should be like

//button[@class='btn btn--05 thm--01'] 

This will work.

3
  • I tried it. But that's not the problem.
    – Syrus
    Commented May 9, 2018 at 8:29
  • Can you please share the HTML code so that I can help you Commented May 9, 2018 at 8:39
  • Thanks for trying to to help. But I could find the mistake and fix the problem
    – Syrus
    Commented May 9, 2018 at 8:40
0

I tried found the solution. There was a little mistake I had done also did a little change in the xpath.
First change the By.className to By.xpath
Then changed the xpath as "//button[contains(@class, 'btn btn--05 thm--01')]"

2
  • This is still very brittle and may break easily in few subsequent app. releases.Always try to use more stable static property like Id/name etc. Commented May 9, 2018 at 8:57
  • Yes, xpath is bit risky but there are no ID's or names availble. Therefore I had to go with the best option available right now
    – Syrus
    Commented May 9, 2018 at 9:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.