{"id":1215,"date":"2018-07-15T15:30:06","date_gmt":"2018-07-15T15:30:06","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1215"},"modified":"2024-12-14T05:46:08","modified_gmt":"2024-12-14T05:46:08","slug":"write-a-java-program-to-capture-auto-suggestions-list","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-capture-auto-suggestions-list\/","title":{"rendered":"Java program to capture auto suggestions list"},"content":{"rendered":"<h2>Java program to capture auto suggestions list<\/h2>\n<p>Write a Java program to capture the auto-suggestions list while entering data on the Google search box.\u00a0 Let us search for &#8220;testingd&#8221; capture the suggestions shown by the Google search engine and print the results as shown in the below picture. Iterator&lt;E&gt; is an interface that is a member of the Java Collections Framework. The iterator is iterated and suggestions are printed in a loop.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1513\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Google-Auto-Suggestion-Box.png\" alt=\"Google Auto Suggestion Box\" width=\"1113\" height=\"603\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Auto Suggestions program<\/strong><\/h3>\n<pre>package com.testingdocs.examples.selenium;\r\n\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\nimport org.openqa.selenium.By;\r\nimport org.openqa.selenium.WebDriver;\r\nimport org.openqa.selenium.WebElement;\r\nimport org.openqa.selenium.firefox.FirefoxDriver;\r\nimport org.openqa.selenium.support.ui.ExpectedConditions;\r\nimport org.openqa.selenium.support.ui.WebDriverWait;\r\nimport org.testng.annotations.AfterClass;\r\nimport org.testng.annotations.BeforeClass;\r\nimport org.testng.annotations.Test;\r\n\r\npublic class AutoSuggestionsExample {\r\n    public WebDriver driver = null;\r\n\r\n    @BeforeClass\r\n    public void initBrowser() {\r\n        System.setProperty(\"webdriver.gecko.driver\", \r\n\"drivers\/geckodriver.exe\");\r\n        driver = new FirefoxDriver();\r\n\r\n    }\r\n\r\n    @Test\r\n    public void Search() throws Throwable {\r\n\r\n        driver.navigate().to(\"https:\/\/www.google.com\");\r\n        driver.findElement(By.name(\"q\")).clear();\r\n        driver.findElement(By.name(\"q\")).sendKeys(\"testingd\");\r\n        WebDriverWait wait = new WebDriverWait(driver, 120);\r\n        wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.\r\n                cssSelector(\".sbsb_a li\"), 0));\r\n\r\n        List&lt;WebElement&gt; suggestionItems = driver.findElements(By.\r\n                cssSelector(\".sbsb_a li\"));\r\n\r\n        Iterator&lt;WebElement&gt; i = suggestionItems.iterator();\r\n        System.out.println(\"Print suggestions for the search box.\");\r\n        while(i.hasNext())\r\n        {\r\n            WebElement element = i.next();\r\n            System.out.println(element.getText());\r\n        }\r\n\r\n    }\r\n\r\n    @AfterClass\r\n    public void closeBrowser() {\r\n        driver.quit();\r\n    }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1514\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Auto-suggestions-Test-method-Java.png\" alt=\"Auto suggestions Test method Java\" width=\"1282\" height=\"689\" title=\"\"><\/p>\n<h2><\/h2>\n<h2><strong>Output of the program<\/strong><\/h2>\n<p>Print suggestions for the search box.<br \/>\ntesting definition<br \/>\ntesting deliverables<br \/>\ntesting data<br \/>\ntesting documents<br \/>\ntesting details<br \/>\ntesting data warehouse<br \/>\ntesting designations<br \/>\ntesting demand in market<br \/>\ntesting documents list<br \/>\ntesting domain<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1515\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Auto-Suggestion-List-Selenium.png\" alt=\"Auto Suggestion List Selenium\" width=\"1122\" height=\"442\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java program to capture auto suggestions list Write a Java program to capture the auto-suggestions list while entering data on the Google search box.\u00a0 Let us search for &#8220;testingd&#8221; capture the suggestions shown by the Google search engine and print the results as shown in the below picture. Iterator&lt;E&gt; is an interface that is a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[24],"class_list":["post-1215","post","type-post","status-publish","format-standard","hentry","category-selenium","tag-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1215"}],"version-history":[{"count":10,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1215\/revisions"}],"predecessor-version":[{"id":26464,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1215\/revisions\/26464"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}