Skip to main content

You are here

List all suggested search terms in Google

The following code opens the browser and navigates to Google. It then types in "Mado" into the search box. A list of search suggestions drop down. The program captures and prints all the suggestions. Allow the progam to finish - you will know when the "WebDriver" printed in the lower right corner of the browser window turns to black. Close the browser window and observe the listed options in the console.

Note: You should have downloaded and set reference to Selenium dot net library from the Selenium HQ website.

    class Program
    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.google.co.uk");  // Actual loading of website
            Console.WriteLine("Extracting all links from Search Suggestion");

            IWebElement searchInput = driver.FindElement(By.ClassName("gbqfif"));

            searchInput.SendKeys("Mado");

            IWebElement searchSuggestionsBox = driver.FindElement(By.XPath("//*[@id='gsr']/table/tbody/tr/td[2]"));
            IList<IWebElement> searchSuggestions = searchSuggestionsBox.FindElements(By.TagName("tr"));
            Console.WriteLine(" Total links: " + searchSuggestions.Count);
            for (int x = 1; x < searchSuggestions.Count; x++)
            {
                Console.WriteLine(searchSuggestions.ElementAt(x).Text);
            }

            Console.ReadLine();
        }
    }

Category: 

Language: 

0
No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

Disclaimer

Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes