2

As of now I am getting the count of the number of matching results using listChanges.size() . How do I directly get the count without loading getChanges in the list?

By getChanges = By.xpath("//td[contains(@class,'blob-code blob-code-addition') or contains(@class,'blob-code blob-code-deletion')]");

List<WebElement> listChanges = driver.findElements(getChanges);

I found this(Count function in XPath) and I tried the below which does not work!

Integer getCount = By.xpath(count("//td[contains(@class,'blob-code blob-code-addition') or contains(@class,'blob-code blob-code-deletion')]"));

Looks like I have to do something like this.

Integer getCount = By.xpath("count(//td[contains(@class,'blob-code blob-code-addition') or contains(@class,'blob-code blob-code-deletion')])");

But the right hand side returns an object of type By

Community
  • 1
  • 1
Zack
  • 2,078
  • 10
  • 33
  • 58
  • can you provide me the `url` to the site you are using if possible? – Saifur Feb 17 '15 at 02:02
  • @Saifur : https://github.com/SunriseSoftVN/qlkh/commit/3aa0e16944e2552a7863941f683f26c571cd464e#diff-33c32c1890dcfb06827b5db4bee85959 – Zack Feb 17 '15 at 02:08
  • So, what are you trying to accomplish there? Just to find out how many matching elements that `xpath` returns? – Saifur Feb 17 '15 at 02:15

2 Answers2

2

You cannot get the count using XPath, because an xpath expression in selenium has to correspond to an actual element on a page.

The way you are doing it via findElements() + size() is how you have to count elements using the selenium webdriver.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2

As alex says, size() is the way to go. However I do have another suggestion for you. Even though the proper way to find the element counts is to use WebDriver api with findElements() as per my knowledge. Another way is to execute javascript by using executeScript() and with proper script. I am not sure if javascript and xpath can be mixed together to accomplish this since xpath execution through javascript is not multi-browser right now. See this. However, I do think using cssSelector with javascript can make it lot easier to accomplish. See the following code :

String cssQuery = ".blob-code-addition, .blob-code-deletion";
String script = "return document.querySelectorAll('" + cssQuery + "').length;";
Object count = ((JavascriptExecutor)driver).executeScript(script);

System.out.println(count);

Print

26

Community
  • 1
  • 1
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • 1
    okay! @Saifur If I could use count with XPATH, it could help me remove the processing associated with this statement. List listChanges = driver.findElements(getChanges); thanks. I will check the above script as well and mark the answer. – Zack Feb 17 '15 at 02:59
  • 1
    @Zack As I said, `List listChanges = driver.findElements(getChanges); listChanges.size();` is the right way. But the above script does the samething and the `cssSelector` is written to replace the `xpath` you are using – Saifur Feb 17 '15 at 03:03