Hello Cherry, below is the automation script for printing all google suggestions for a keyword in Selenium:
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.gecko.driver","C:\\Users\\Abha_Rathour\\Downloads\\ExtractedFiles\\geckodriver-v0.24.0-win64\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/webhp?complete=1&hl=en");
// Enter the query string “Cheese”
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("Cheese");
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
WebElement resultsDiv = driver.findElement(By.className("aajZCb"));
// If results have been returned, the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
break;
}
}
// And now list the suggestions
List<WebElement> allSuggestions = driver.findElements(By.xpath("/html/body/div/div[3]/form/div[2]/div/div[2]/div[2]/ul"));
for (WebElement suggestion : allSuggestions) {
System.out.println(suggestion.getText());
}
}