Why would StringUtils return the incorrect # of matches of a substring?

236 views Asked by At

I am using StringUtils from the apache commons library to check the # of matches of a substring within the source code of an html page.

I have converted the page source using the webdriver command:

String pageSource = driver.getPageSource();    

The code I am using to find # of matches is:

int cloudfCount = StringUtils.countMatches(pageSource, "cloudfront");
System.out.println("There are " + cloudfCount + " instances of cloudf text found within the page source.");

When I perform the check manually, I get 2 matches. Which is what I am expecting.

But when I use the above code in an automated Selenium script, I am getting 5 results.

Any thoughts/ideas on where I'm going wrong?

Thank you.

1

There are 1 answers

1
Vivek Singh On

@kevin it would be great if you could provide us with contents of pagesource. Another thing, if the page has been modified it wont be necessary you get the current page source, pls refer getPageSource Doc

If anyhow getPageSource ain't working and you need the text within body tag, you could use: document.getElementsByTagName('body')[0].textContent using JavascriptExecutor,

and then try out StringUtils.countMatches().

Thanks.