'Webdriver: How can i verify whether a text is present in a page

I want to know how can i verify whether a text is present in a page. I would like to verify whether "Google.co.in offered in" is present in the page

static  WebDriver driver= new FirefoxDriver();
public void test() {
        driver.manage().window().maximize();
        driver.get("http:google.co.in/");
}

Do there any alternate to looking in page source?



Solution 1:[1]

Step 1 Use locate by xpath for the path - //*[contains(.,.)] to locate the element

Step 2 then use getText() - this will return all 'visible' text on page

Solution 2:[2]

try with

if(driver.findElement(By.cssSelector("#_eEe")).getText().contains("Google.co.in offered in"))
   return true;
else
   return false;

Explanation: in google.co.in page the provided text is present under element with css - #_eEe. So driver will search for the element by css followed by getText which will return the text contained in that element. Since getText returns String object you can apply contains function to check for your desired text.

Solution 3:[3]

Please try the below JAVA code for verifying the presence of text "Google.co.in offered in" in the page:

if(driver.getPageSource().contains("Google.co.in offered in"))
    System.out.println("Text is present in the page");
else
    System.err.println("Text is not present in the page");

NOTE: You can replace the text 'Google.co.in offered in' with whatever text you want to search for in the concerned webpage.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amrit
Solution 2
Solution 3 Subh