How to print all google suggestions for a keyword using Selenium Webdriver?

0 votes
How to print all google suggestions for a keyword using Selenium Webdriver?
May 22 in Selenium by Cherry
228 views

1 answer to this question.

0 votes

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

}

}
answered May 22 by Abha
• 25,740 points

Related Questions In Selenium

+2 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
800 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,040 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
743 views
0 votes
1 answer
0 votes
1 answer

How to click on a hyperlink using Selenium WebDriver?

Hi Jonathan, you can use click() method in ...READ MORE

answered May 29 in Selenium by Abha
• 25,740 points
374 views