I need to verify a Text present in the page through WebDriver. I like to see the result as boolean (true or false). Can any one help on this by giving the WebDriver code?
-
2In what programming language? What browser? Does the text always appear in the same element? Where? What element? – Arran Nov 05 '12 at 14:18
8 Answers
As zmorris points driver.getPageSource().contains("input");
is not the proper solution because it searches in all the html, not only the texts on it.
I suggest to check this question: how can I check if some text exist or not in the page?
and the recomended way explained by Slanec:
String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue("Text not found!", bodyText.contains(text));

- 1
- 1

- 1,626
- 1
- 18
- 34
-
Shouldn't it be: String bodyText = driver.findElement(By.tagName("body")).getText(); Assert.assertTrue(true, bodyText.contains(text)); – Cagy79 Aug 18 '15 at 22:49
Yes, you can do that which returns boolean. The following java code in WebDriver with TestNG or JUnit can do:
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 below:
assertTrue(isTextPresent("Your text"));
Or, there is another way. I think, this is the better 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());
}
-
Your question seems to be lacking some information. 1) What language are you using? 2) Are you using any test framework as JUnit or TestNG? – Jan 31 '13 at 07:39
-
-
This is a better solution as it can be leveraged for multiple text values – Bob Small Aug 12 '16 at 17:55
-
Driver.getPageSource() is a bad way to verify text present. Suppose you say, driver.getPageSource().contains("input");
That doesn't verify "input" is present on the screen, only that "input" is present in the html, like an input tag.
I usually verify text on an element by 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 text match, just remove the contains function:
driver.findElement(By.xpath("//*[text()='someText']));

- 1,057
- 2
- 12
- 23
If you are not bothered about the location of the text present, then you could use Driver.PageSource property as below:
Driver.PageSource.Contains("expected message");

- 315
- 1
- 6
-
Is there any way of finding the text without page source and more over i need to print True if the text exists any where in the page and False if the text doen't... – kranthi kumar Nov 05 '12 at 13:47
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");
}

- 7,798
- 4
- 39
- 47

- 83
- 7
For Ruby programmers here is how you can assert. Have to include Minitest to get the asserts
assert(@driver.find_element(:tag_name => "body").text.include?("Name"))

- 2,196
- 1
- 13
- 17
Below code is most suitable way to verify a text on page. You can use any one out of 8 locators as per your convenience.
String Verifytext= driver.findElement(By.tagName("body")).getText().trim(); Assert.assertEquals(Verifytext, "Paste the text here which needs to be verified");

- 1
- 3
If you want check only 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;
}

- 11
- 3