There are certain types of textbox designed for browser apps which provides suggestions based on the string typed,
The suggestions may be based on predefined options (e-commerce category or flight booking sites) or based on previous searches done (like on google searchbox).
Let’s see how to capture google suggestion options through code:
public class Suggestions { static WebDriver driver; @Test public static void googleSuggestion() throws InterruptedException { String txt = "selenium"; System.setProperty("webdriver.chrome.driver", "./chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://google.co.in"); driver.manage().window().maximize(); driver.findElement(By.id("lst-ib")).sendKeys(txt); Thread.sleep(2000); WebElement suggestion = driver.findElement(By.className("sbsb_b")); List<WebElement> options = suggestion.findElements(By.tagName("li")); String Suggtext = null; for(WebElement option : options) { Suggtext = option.findElement(By.className("sbqs_c")).getText(); System.out.println(Suggtext); if(Suggtext.contains(txt)) System.out.println("matched"); else System.out.println("not matched"); } } }
To explain this, have a look at the screenshot, which shows the html part of suggestion options, it’s not dropdown options rather list items
class = sbsb_b contains all the list options, so 1st get the webelement suggestion
now let’s grab all the list options in a List, variable options
each list item have one webElement common, that is class=sbqs_c which actually contains the text we are looking for, that’s it
Let’s grab the text and do whatever you want!
If we want to select the required list option, then just click the respective option webelement inside the for loop.
option.click();
Same way, you can try with “FROM” field of http://www.jetairways.com/EN/IN/Home.aspx