0

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?

Arjun
  • 321
  • 3
  • 9
  • 20
  • 2
    possible duplicate of [How to verify a Text present in the loaded page through WebDriver](http://stackoverflow.com/questions/13226986/how-to-verify-a-text-present-in-the-loaded-page-through-webdriver) – t0mppa Dec 15 '14 at 06:26

3 Answers3

0

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.

Vivek Singh
  • 3,641
  • 2
  • 22
  • 27
  • A little explanation will help understand your code! – Sakshi Singla Dec 15 '14 at 08:31
  • The solution provided is for the specific criteria in which @Arjun has asked for "Google.co.in offered in". The explanation is updated in answer. – Vivek Singh Dec 15 '14 at 08:57
  • Hi @VivekSingh , I got this. But in general how can I do this? means, if i want to search for any text in any site? Any alternate other than cssSelectorr? – Arjun Dec 15 '14 at 09:43
  • You can use getPageSource. But this function is specific to complete page, i.e. it will give you complete data even if the line you are searching might be embedded in some scripts tag also or might not be visible i.e. embedded in hidden elements. You can also use `document.body.textContent.contains('Google.co.in offered in')` in JavascriptExecutor. – Vivek Singh Dec 15 '14 at 10:17
  • Its not necessary u go with cssSelector you can use any other alternative like xpath, name, id, etc based on the element you are searching for. Though my answer is specific to an element only. – Vivek Singh Dec 15 '14 at 10:22
0

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

Amrit
  • 433
  • 3
  • 19
0

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.

Subh
  • 4,354
  • 1
  • 13
  • 32