Search This Blog

Friday, 23 October 2015

How to Print Google Search result in the Eclipse Console?

Example to extract the text from google search result

To extract the search result count (which is displayed on the top) from the google search result, we have to

  1.  Find the WebElement of the search result and extract the WebElement
  2. The resulted string (result) is having extra characters like "About 5050 results"
  3. Extract the sub string from the obtained result string
  4. Replace all the extra characters from the result string
  5. Now parse the String to number format to convert the string to number.
GitHub Url

The program for the above example is as below



import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class GoogleSearchResult {
    public static void main(String[] args) throws InterruptedException {
        Selenium selenium = new DefaultSelenium("localhost", 4444, "firefox", "http://www.google.com/");
        selenium.start();
        selenium.open("/?gws_rd=ssl");
        selenium.type("//input[@id='lst-ib']", "test automation 4 all");
        selenium.click("name=btnG");
//        selenium.waitForPageToLoad("30000");
        Thread.sleep(2000);
        String result = selenium.getText("id=resultStats");
        System.out.println("The Raw format" + result);

        result = result.substring(7, result.indexOf("result"));
        System.out.println("After extracting the substring of the result : " + result);

        result = result.replaceAll(",", "").trim();
        System.out.println("Removing the extra characters from the remaining string " + result);

        System.out.println("Now the remaining string is in the String format. Converting them to the Integer format");
        int searchResultCount = Integer.parseInt(result);

        System.out.println("Total Number of search results : " + searchResultCount);

        selenium.stop();
    }

}

2 comments:

  1. Can anybody give me the sample code for extracting text from images using java selenium webdriver

    ReplyDelete
  2. Hello Ramesh,
    The process of extracting the text from the image is mainly dependent on character recognition. This is implemented in couple of Java OCR.

    Please take help of this documentation for a better understanding.
    http://roncemer.com/software-development/java-ocr/

    Feel free to post your comment.

    Thanks.

    ReplyDelete