Suppose I have an add question facility but can't add same question in the form. So need to check whether the question is already visible on the screen, if not then add it.

I am not getting how to match a string value to the content of a form. is it possible to do so with selenium webdriver. Or if there is any other way to achieve this? I just need to check a text whether displayed on the screen or not.

2 Answers 2


When you use driver.getPageSource() it gives you entire page source. To be more efficient, use something like this:

WebElement element = driver.findElement(By.xpath("//*[contains(.,.)]"));

return element.getText.contains(data);


Something like this should work:

public boolean isTextOnPagePresent(String text) {
    WebElement body = driver.findElement(By.tagName("body"));
    String bodyText = body.getText();
    return bodyText.contains(text);

Currently I have no possibility to test that. If it should not work, you could try to use getPageSource() instead of get the text of the body tag. But this does not furfill the "on the visible html page" part of your question.

  • In this case I think the visible part is irrelevant unless all questions are in the body allready. Looping thru all visible elements and checking getting the text is more complex and time consuming. +1 for quick and dirty :) Jul 15, 2014 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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