How To Select from the various options in a search box?

39 views
Skip to first unread message

Litl Programmer

unread,
Jul 10, 2016, 12:21:08 AM7/10/16
to Selenium Users
Hi,

I am new to selenium and I have a query. While working on a hotel booking website called "Trivago.com", I entered a city in the search box and several options appeared as a drop down list. Can you suggest, how can I pick up one of these options and then select the search button? Here's the code that I have written so far. It types the name in the search box but doesn't select the option. Please guide.

        System.setProperty("webdriver.chrome.driver", "C:\\Selenium Drivers\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("http://www.trivago.com");
        System.out.println(driver.getTitle());
        driver.findElement(By.id("js_querystring")).sendKeys("Tampa");//alternate way of doing
        driver.findElement(By.xpath(".//*[@id='js-dealform-suggest']/ul/li[1]/div[2]/span[1]/strong")).click();
        driver.findElement(By.xpath(".//*[@id='js_go']")).click();
 
The page I am working on is "http://www.trivago.com/".

Anil Karthik

unread,
Jul 13, 2016, 4:13:52 PM7/13/16
to Selenium Users
To get the drop-down list options in your scenario .You should use custom xpath(dynamic xpath). I using  this xpath("//li/div/span[@class='ssg-title']")
These list of options are stored in list and use for loop to iteration
see the below source code. 

 
System.setProperty("webdriver.chrome.driver", "D:\\training doc\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.trivago.com");
System.out.println(driver.getTitle());
driver.findElement(By.id("js_querystring"))
.sendKeys("Tampa");/* alternate way of doing */
List<WebElement> dropdownlist = driver.findElements(By.xpath("//li/div/span[@class='ssg-title']"));
for (int i = 0; i <= dropdownlist.size(); i++) {
if (dropdownlist.get(i).getText().equalsIgnoreCase("Tampa Marriott Waterside Hotel & Marina, Tampa")) {

dropdownlist.get(i).click();
i = dropdownlist.size();
}

}
Reply all
Reply to author
Forward
0 new messages