Peter Penzov Peter Penzov - 8 months ago 69
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();
WebElement searchBox = driver.findElement("q"));

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?


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