How to automate Google Home Page auto suggestion?

To extract the Auto Suggestions from the Search Box on Google Home Page you have to induce WebDriverWait for the visibilityOfAllElements and you can use the following solution:

  • Code Block:

    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Google_Auto_Suggestions {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars"); 
            options.addArguments("--disable-extensions"); 
            WebDriver driver = new ChromeDriver(options);
            driver.get("http://www.google.com");
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys("motogp");
            List<WebElement> motolist = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//form[@action='/search' and @role="search"]//ul[@role="listbox"]//li//span")));
            for(WebElement list:motolist)
            {
                String text=list.getText();
                System.out.println(text);
            }
        }
    }
    
  • Console Output:

    Only local connections are allowed.
    Dec 04, 2018 6:14:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    motogp
    motogp 2018
    motogp live
    motogp results
    motogp game
    motogp news
    motogp bikes
    motogp race
    motogp wiki
    motogp schedule
    
  • Browser Snapshot:

motogp_google_search_results

Here you can find a relevant discussion on Python Selenium Testing. How can I extract the Auto Suggestions from search box on Google Home Page?

Leave a Comment