java - How many times a text appears in webpage - Selenium Webdriver -


hi count how many times text ex: "vim liquid marathi" appears on page using selenium webdriver(java). please help.

i have used following check if text appears in page using following in main class

assertequals(true,istextpresent("vim liquid marathi"));

and function return boolean

protected boolean istextpresent(string text){     try{         boolean b = driver.getpagesource().contains(text);         system.out.println(b);         return b;     }     catch(exception e){         return false;     } } 

... not know how count number of occurrences...

the problem using getpagesource(), there id's, classnames, or other parts of code match string, don't appear on page. suggest using gettext() on body element, return page's content, , not html. if i'm understanding question correctly, think more looking for.

// text of body element webelement body = driver.findelement(by.tagname("body")); string bodytext = body.gettext();  // count occurrences of string int count = 0;  // search string within text while (bodytext.contains("vim liquid marathi")){      // when match found, increment count     count++;      // continue searching left off     bodytext = bodytext.substring(bodytext.indexof("vim liquid marathi") + "vim liquid marathi".length()); } 

the variable count contains number of occurrences.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -