javaseleniumselenium-webdriverxpathnavigator

not able to automate drop down as while selecting one value gets auto picked,so second one is not visible


I am trying to automate below website drop down:

https://qa.roofandfloor.com/

In the city section,we have both bangalore and chennai,suppose bangalore gets autoselected,so running below code is failing:

WebElement element=driver.findElement(By.xpath("//select[@class='city-dropdown-search form-city hide select2-offscreen']"));
    Select se=new Select(element);
    se.selectByIndex(2);

its not able to recognise second city,please help me whats wrong I am doing here.

html tag is as below:

<select id="combobox2" class="city-dropdown-search form-city hide select2-  offscreen" name="city" tabindex="-1">
 <option value="Chennai">Chennai</option>
<option selected="" value="Bangalore">Bangalore</option>
</select>

is there problem with xpath or something else I am missing here


Solution

  • You can actually type in that as below using sendKeys()

    WebElement toElement = driver.findElement(By.id("s2id_combobox2"));
    toElement.click();
    driver.findElement(By.id("select2-drop")).sendKeys("Bangalore");
    

    Here you can pass on if you have more cities by just changing the value in sendKeys()