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, 2019 in Selenium by Cherry
5,294 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 23, 2019 by Abha
• 28,140 points

Related Questions In Selenium

+2 votes
1 answer
0 votes
1 answer

How can I automate google search for a keyword using ruby selenium webdriver?

Hi Amir, following code snippet shows how ...READ MORE

answered Aug 26, 2019 in Selenium by Anvi
• 14,150 points
1,597 views
0 votes
2 answers

How to get all options of a dropdown using python selenium webdriver?

WebElement element = driver.findElement(By.id("")); Select select = new ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
17,192 views
0 votes
2 answers

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,500 points
13,674 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
0 votes
1 answer

How to get all options of a dropdown on a webpage using Selenium Webdriver?

Hey Joel, you can use following lines ...READ MORE

answered Jul 15, 2019 in Selenium by Abha
• 28,140 points
2,519 views
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, 2019 in Selenium by Abha
• 28,140 points
7,239 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP