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

asked May 9, 2018 at 8:20

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.

Bence Kaulics
1,00712 silver badges22 bronze badges
answered May 9, 2018 at 8:25
3
  • I tried it. But that's not the problem. 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 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')]"

answered May 9, 2018 at 8:43
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 Commented May 9, 2018 at 9:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.