3

I'm trying to count the number of rows within a data grid on a webpage using Selenium. Each row is represented by a div using the following structure (simplified for this post) :-

<div id="resultsGrid">
    <div>
        <div>
            <div/>
            <div>
                <div class="canvas">
                    <div data-bind="foreach: renderedRows">
                        <div>list item</div>
                        <div>list item</div>
                        <div>list item</div>
                        <div>list item</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

From reading other similar posts I've tried using the following code and XPath to return the number of div's within the <div data-bind="foreach: renderedRows">

var rowCount = webDriver.FindElements(By.XPath("//*[@id='usersResultsGrid']/div/div/div[2]/div/div")).Count;

Whenever I try this I just get a count of 1 returned, not the expected 4 (in this example). Can anyone point in the right direction as to what I am doing wrong?

Thanks.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Matt
  • 43
  • 1
  • 5
  • Resolved. The answer was to add an additional div to the xpath as in By.XPath("//*[@id='usersResultsGrid']/div/div/div[2]/div/div/div"). My initial xpath was pointing to the div 'data-bind' element which of course only has a count of 1. – Matt Dec 18 '20 at 07:31

3 Answers3

1

Resolved. The answer was to add an additional div to the xpath as in By.XPath("//*[@id='usersResultsGrid']/div/div/div[2]/div/div/div"). My initial xpath was pointing to the div 'data-bind' element which of course only has a count of 1, and I needed the list of div's below it.

Matt
  • 43
  • 1
  • 5
0

Use below any of the xpath which will identify all four elements.

//div[@id='resultsGrid']//div[@class='canvas']/div[@data-bind]/div

Or

//div[@id='resultsGrid']//div[@data-bind]/div

Effectively your code will be

var rowCount = webDriver.FindElements(By.XPath("//div[@id='resultsGrid']//div[@class='canvas']/div[@data-bind]/div")).Count;

KunduK
  • 32,888
  • 5
  • 17
  • 41
0

To count the number of rows within the data grid using Selenium you can use either of the following Locator Strategies:

  • CssSelector:

    var rowCount = webDriver.FindElements(By.XPath("div#resultsGrid div.canvas div[data-bind$='renderedRows'] > div")).Count;
    
  • XPath:

    var rowCount = webDriver.FindElements(By.XPath("//div[@id='resultsGrid']//div[@class='canvas']/div[contains(@data-bind, 'renderedRows')]/div")).Count;
    

Ideally, you have to induce WebDriverWait for the desired VisibilityOfAllElementsLocatedBy() and you can use either of the following Locator Strategies:

  • CssSelector:

    var rowCount = new WebDriverWait(webDriver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("div#resultsGrid div.canvas div[data-bind$='renderedRows'] > div")));
    
  • XPath:

    var rowCount = new WebDriverWait(webDriver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//div[@id='resultsGrid']//div[@class='canvas']/div[contains(@data-bind, 'renderedRows')]/div")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Tried both methods and get rowCount = 0. I put a check in front of the evaluation to ensure it has loaded in the DOM, even put a hard wait in there just to be totally sure, but still get a rowCount of 0. – Matt Dec 17 '20 at 20:17