Count how many anchors(links) in a div

So I search and found some post regarding this but none of them worked for me. here is a layout of the html (changed for NDA reasons):
The Div is a wrapper, but it’s the one that contains all the links

<div class="abcd" id = "apbox">

  <a href = "javascript:abc('123')...</a>
  <a href = "javascript:abc('124')...</a>
  <a href = "javascript:abc('125')...</a>
  <a href = "javascript:abc('126')...</a>
  <a href = "javascript:abc('127')...</a>

</div>

Here is what I have tried:

WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> link = driver.findElements(By.xpath("//*[@id='apbox']/a"))
System.out.println(link.size())

print out is 0

I tried basic link searches on the page using By.tagName(“a”) and it returns a value. Help would be greatly appreciated.

What is ::before and ::after? That is CSS code.

yah CSS, doesnt really have anything to do with what i need ; > sorry

the output i get is here:
2021-01-22 23:06:12.522 DEBUG testcase.test - 7: driver = getWebDriver()
2021-01-22 23:06:12.522 DEBUG testcase.test - 8: list = driver.findElements(By.xpath("//*[@id=‘notification-box’]/a"))
2021-01-22 23:06:12.630 DEBUG testcase.test - 9: out.println(list.size())
0
2021-01-22 23:06:12.639 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/CORE2_Welcome_Dash/test

String js = 'return document.querySelectorAll("#apbox a[href]").length;'
int length = (int) WebUI.executeJavaScript(js, null)

Here it is running against this forum page:

@Russ_Thomas thanks i tried it on this page and it worked, but when i applied it to our web app it returned an uncaught syntax error

image

when you do it in console, don’t use the return statement, just the document.blah snippet.

1 Like

@johentie So is it supposed to be:

By.xpath("id('notification-box')]/a"))

or

By.xpath("id('apbox')]/a"))

1 Like

got it working… just need to figure out how to put his part into the testcase
Thank you

aight ive been using katalon for like a week officially.

am i suppose to make a keyword using what @Russ_Thomas wrote?

String js = 'return document.querySelectorAll("#apbox a[href]").length;'
int length = (int) WebUI.executeJavaScript(js, null)

so when i enter document.querySelectorAll("#apbox a[href]").length; in console it shows 5.

to get that to print out in a katalon test case seems to be more difficult then it seems

i put it in the script tab like this:

    String js = 'return document.querySelectorAll("#appointment-box a[href]").length;'
    int length = (int) WebUI.executeJavaScript(js, null)
    println(length)

and the value of length is still 0

2021-01-24 23:09:29.225 DEBUG t.View_more_appointments_loads_5 - Copy - 8: js = “return document.querySelectorAll(”#appointment-box a[href]“).length;”
2021-01-24 23:09:29.226 DEBUG t.View_more_appointments_loads_5 - Copy - 9: length = executeJavaScript(js, null)
2021-01-24 23:09:29.267 DEBUG t.View_more_appointments_loads_5 - Copy - 10: println("the number of appointments is: " + length)
the number of appointments is: 0

Try it in the browser console, like I mentioned above. If it comes back zero, that’s length - which means you’re probably looking at the wrong container.

You showed me apbox, then changed it. I can’t help without seeing you actual HTML from the page.

@johentie

You mentioned 2 IDs: #apbox and #appointment-box.

You seem to be talking about these 2 IDs as if they are pointing to the same HTML element. But it can’t be the case. I guess that #apbox is right but #appointment-box is wrong (not present).

@kazurayam sorry for the. confusion ; >
I posted my actual ID i was using (appointment-box). So apbox was just made up. But now that you know #appointment-box is my real ID i am using.

When I do what @Russ_Thomas showed me in the browser I get return of 5 (see below screenshot)

Screen Shot 2021-01-25 at 10.38.40 AM

But when i run it in the testcase it returns 0 using the following

String js = 'return document.querySelectorAll("#appointment-box a[href]").length;'
int length = (int) WebUI.executeJavaScript(js, null)
println(length)
1 Like

When a human (YOU!) uses the browser console, there is a lot of time spent moving around and entering values in the console. That time allows the browser time to render the HTML on the page.

When Katalon runs your test case code, it runs SUPER FAST. There is very little time given to the browser to finish rendering the page.

In your test case code, WAIT until the the elements you need are VISIBLE:

@Russ_Thomas This is exactly what I thought it might be returning 0, so i entered a wait till page loads, and i even did a verify check if element exists for #appointment-box before running the JS portion.

Maybe I will wait for element visible for one of the appointments to load as well.

Always wait. Wait for EVERYTHING you need to verify/click/setText… EVERYTHING.

1 Like

Patience is a virtue :smiley:

And a requirement :wink:

Virtue is not required in real life, unfortunately.

You were right…
did a wait for the first a href to be visible and it worked… sigh… the joys of automation

Thank you @Russ_Thomas and @anon46315158

1 Like