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
@
is missing from the Xpath you have provided:
It should be like
//button[@class='btn btn--05 thm--01']
This will work.
-
I tried it. But that's not the problem.Syrus– Syrus2018年05月09日 08:29:46 +00:00Commented May 9, 2018 at 8:29
-
Can you please share the HTML code so that I can help youPrasanna venkatesh– Prasanna venkatesh2018年05月09日 08:39:17 +00:00Commented May 9, 2018 at 8:39
-
Thanks for trying to to help. But I could find the mistake and fix the problemSyrus– Syrus2018年05月09日 08:40:50 +00:00Commented May 9, 2018 at 8:40
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')]"
-
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.Vishal Aggarwal– Vishal Aggarwal2018年05月09日 08:57:37 +00:00Commented 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 nowSyrus– Syrus2018年05月09日 09:26:40 +00:00Commented May 9, 2018 at 9:26
Explore related questions
See similar questions with these tags.