How to check the text present on the loaded page through WebDriver

I need to check the text present on the page through WebDriver. I like to see the result as boolean (true or false). Can someone help with this by specifying WebDriver code?

+7
source share
8 answers

Since the points are zmorris driver.getPageSource().contains("input"); are not the right solution, because it searches in all html, not just the texts on it. I suggest checking this question: how to check if there is any text on the page? and the recommended method explained by Slanec:

 String bodyText = driver.findElement(By.tagName("body")).getText(); Assert.assertTrue("Text not found!", bodyText.contains(text)); 
+5
source

Yes, you can do what boolean returns. The following java code in WebDriver with TestNG or JUnit can execute:

 protected boolean isTextPresent(String text){ try{ boolean b = driver.getPageSource().contains(text); return b; } catch(Exception e){ return false; } } 

Now call the above method as shown below:

 assertTrue(isTextPresent("Your text")); 

Or, there is another way. I think this is the best way:

 private StringBuffer verificationErrors = new StringBuffer(); try { assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]* Your text here\r\n\r\n[\\s\\S]*$")); } catch (Error e) { verificationErrors.append(e.toString()); } 
+3
source

If you are not worried about the location of the text present, you can use the Driver.PageSource property, as shown below:

Driver.PageSource.Contains ("expected message");

+1
source

Driver.getPageSource () is a bad way to check for text. Suppose you said: driver.getPageSource().contains("input"); This does not check that "enter" is present on the screen, only that "input" is present in html, like an input tag.

I usually check the text on an element using xpath:

 boolean textFound = false; try { driver.findElement(By.xpath("//*[contains(text(),'someText')]")); textFound = true; } catch (Exception e) { textFound = false; } 

If you want an exact match for the text, just remove the contains function:

 driver.findElement(By.xpath("//*[text()='someText'])); 
+1
source

For Ruby programmers, here's how you can argue. You need to enable Minitest for claims.

  assert(@driver.find_element(:tag_name => "body").text.include?("Name")) 
0
source

Below code is the most appropriate way to check the text on the page. You can use any of 8 locators, as you wish.

String Verifytext = driver.findElement (By.tagName ("body")). getText (). trim (); Assert.assertEquals (Verifytext, "Paste text here that needs to be verified");

0
source

If you want to check only the displayed objects ( C # ):

  public bool TextPresent(string text, int expectedNumberOfOccurrences) { var elements = Driver.FindElements(By.XPath(".//*[text()[contains(.,'" + text + "')]]")); var dispayedElements = 0; foreach (var webElement in elements) { if (webElement.Displayed) { dispayedElements++; } } var allExpectedElementsDisplayed = dispayedElements == expectedNumberOfOccurrences; return allExpectedElementsDisplayed; } 
0
source

Note: not in boolean

 WebDriver driver=new FirefoxDriver(); driver.get("http://www.gmail.com"); if(driver.getPageSource().contains("Ur message")) { System.out.println("Pass"); } else { System.out.println("Fail"); } 
-one
source

All Articles