Peter Penzov Peter Penzov - 5 months ago 27
Java Question

Get all search results from Google search page

I want to get the search result from google after I type some string for search. How I can do this with Selenium? So far I managed to create this:

WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000);
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();

System.out.println("Current Url: " + driver.getCurrentUrl());

List<WebElement> results = driver.findElements(By.xpath("//h3[@class=r]/a"));

for(int i=0; i<results.size(); i++){
System.out.println(">>>>> results " + results.get(i).getText());
}

// second attempt

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"));
}


How I can solve this?

Answer

You need to wait for the results to appear after submitting the search form:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("h3.r a")));
Comments