cooksey
cooksey

Reputation: 75

Java and Selenium best way to display Google search results

I am very new to Selenium and Java but I am trying to make a simple program that loads google, does a search and then displays how many results and how long it took to load the google search. I am running into issues with the results, and getting that to display

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class GoogleSearch {

public static void main(String[] args) {

    FirefoxDriver driver = new FirefoxDriver();
    driver.get("http://google.com");


    WebElement searchTextBox = driver.findElement(By.id("lst-ib"));
    searchTextBox.sendKeys("Colin");

    String pageUrl = driver.getCurrentUrl();
    System.out.println(pageUrl);

    WebElement searchButton = driver.findElement(By.className("lsb"));
    searchButton.click();

    String pageTitle = driver.getTitle();
    System.out.println("Page title is:" + pageTitle);


    WebDriverWait wait = new WebDriverWait(driver, 5);// 5 seconds 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("resultsStats")));
    driver.findElement(By.id("resultsStats"));

    WebElement results = driver.findElement(By.id("resultsStats")); //displays # of results from search
        System.out.println(results);

    //driver.quit();


}
}

Here are the results I am getting:

https://www.google.com/?gws_rd=ssl
Page title is:Google
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"resultsStats"}
Command duration or timeout: 13 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'

Upvotes: 1

Views: 441

Answers (1)

Atul Dwivedi
Atul Dwivedi

Reputation: 1462

It's not resultsStats, it is resultStats. Change the same and you are good to go!

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("resultStats")));
driver.findElement(By.id("resultStats"));

WebElement results = driver.findElement(By.id("resultStats")); //displays # of results from search

resultStats

Upvotes: 2

Related Questions