user3655432
user3655432

Reputation: 25

Capturing the Title of each Search results in Google Search Page using Selenium Web Driver

I am doing a project where I have to compare the Google search results with another search results engine. I am planning to perform this testing by comparing the search results using Selenium tool.

What I want to do is:

  1. Capture the first 20 search results from Google. Save it in a csv file
  2. Capture the first 20 search results from other search engine. Save it in csv file.
  3. Compare the results.

Now i got a code from stackoverflow site as below. Modified to my requirement. It is fetching the first result's title and link. How do i get the title and link of the entire page?

My code is:

    package gooPkg;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class testSearch {

    public static void main(String[] args) {
        String baseUrl = "https://www.google.co.in/";
        WebDriver driver = new FirefoxDriver();
        //String underConsTitle = "Under Construction: Mercury Tours";
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get(baseUrl);
        // Enter the query string "Cheese"
        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("obama twitter");
        query.submit();

        long end = System.currentTimeMillis() + 5000;
        while (System.currentTimeMillis() < end) {
        WebElement resultsDiv = driver.findElement(By.className("gssb_e"));

        if (resultsDiv.isDisplayed()) {
            break;
        }
    }

        List<WebElement> weblinks = driver.findElements(By.xpath("/html/body/div/div[3]/div[2]/div[6]/div/div[4]/div/div[2]/div[2]/div/ol/li/div/div/h3/a"));
       for (WebElement suggestion : weblinks) {
        System.out.println(suggestion.getText()+"\n");
        System.out.println("==> "+suggestion.getAttribute("href")+"\n");

    }
}}

Upvotes: 2

Views: 4896

Answers (1)

Santoshsarma
Santoshsarma

Reputation: 5667

You can use findElements to get the all the search result titles & links.

String baseUrl = "https://www.google.co.in/";
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get(baseUrl);
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("obama twitter");
query.submit();

//==============Here logic goes =======================
//get all the search result 
List<WebElement> allSearchResults=driver.findElements(By.cssSelector("ol li h3>a"));

//iterate the above list to get all the search titles & links from that page
for(WebElement eachResult : allSearchResults) {
System.out.println("Title : "+eachResult.getText()+", Link : "+eachResult.getAttribute("href"));
}

EDIT-I

Try with below logic. Include search term in url itself.

String baseUrl = "https://www.google.co.in/#q=webdriver";
driver.get(baseUrl);

//get all the search result 
List<WebElement> allSearchResults=driver.findElements(By.cssSelector("ol li h3>a"));

    //iterate the above list to get all the search titles & links from that page
for(WebElement eachResult : allSearchResults) {
    System.out.println("Title : "+eachResult.getText()+", Link : "+eachResult.getAttribute("href"));
}

Upvotes: 1

Related Questions