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 right-click within the page, view source, then ctrl-f to "find" cloudfront, 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?

Could it somehow be related to the fact that the "cloudfront" text is part of 2 javascript tags within the page source?

Thank you.

  • Do a ctrl + f on the page source given by webdriver and the one in your browser. See the difference. – LittlePanda Apr 20 '15 at 10:14

You are comparing apples and oranges. Selenium works off the DOM, not off the page source. For a more fair comparison, try opening the browser debugger and searching for "cloud front".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.