Ask a Question

Getting the count of elements on the page

tvklovesu
Contributor

Getting the count of elements on the page

Hi there,

I am trying to get the count of this object from the webpage and run the loop. The Count is returning null for me when I inspect in the console I have around 9. Also for this FindElements when I try to use the library functions its taking for ever to load and shows only spin.

 

tvklovesu_0-1664983663935.png

 

Source code

tvklovesu_0-1664990769478.png

 

2nd option

var TestedPage, CSSSelector

CSSSelector = "button[title='More options']"

TestedPage = NameMapping.Sys.browser.pageRavenCore.FindElements("//div[@data-testid='canvasItem']")

if I use TestedPage.QuerySelector(CSSSelector) I am getting the following error.

 

tvklovesu_1-1664985362079.png

 

3rd option

for the following script I was able to get the object but still when I say object.Count its returning empty


 var res;

Aliases.canvas_card_Item
res= Aliases.canvas_card_Item.FindElements("button[title='More options']");
// Check the result
if (!strictEqual(res, null))
// If the element was found, output its HTML code
Log.Message(res.Count);
else
// If the element was not found, post a warning to the log
Log.Warning("The element was not found.");

 

4th option

var TestedPage

TestedPage = NameMapping.Sys.browser.pageRavenCore.FindElements("//div[@data-testid='canvasItem']")
TestedPage.Count
Log.Message("canvasCard selector "+ TestedPage.Count) It printed undefined

2 REPLIES 2
rraghvani
Super Contributor

Here's an example,

 

function Test1()
{
    var page = Sys.Browser().Page("https://services.smartbear.com/samples/TestComplete15/smartstore");
    var artlist = page.FindElement("//section//div[contains(@class, 'artlist')]");
    
    var res = artlist.QuerySelectorAll("article");
    if (!strictEqual(res, null)) {
        Log.Message("Number of items found: " + res.length);
    
        for (var i = 0; i < res.length; i++) {
            Log.Message(res[i].contentText);
        }
    } else {
        Log.Warning("The element was not found");
    }
}

 

Hi @rraghvani, I used the same document to get the code snippet and changed according to my requirement. But any how thanks for looking in to this. Actually I was able to fix the issue my self that the testedPage object I am using the wrong parent element. There is one more level up parent which refers to the complete frame, and after referring that element then I was able to get the correct count.

The fix is

CSSSelector = "div[data-testid='canvasItem']"

TestedPage = NameMapping.Sys.browser.pageRavenCore.canvas_Window

Using this the TestedPage.QuerySelectorAll(CSSSelector) returned the correct count

cancel
Showing results for 
Search instead for 
Did you mean: