Using a select-box with Selenium

Issue

I am trying to find a suitable way for Selenium to select an item within a select-box.

I have tried using:

new Select(driver.findElement(By.xpath("//select-box[@name='countryCode']"))).selectByVisibleText(country);

but get the expected error of:

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "select-box"

Trying to use WebElement works great to select the element, but not able to "search" the results for the correct item list.

This is the HTML I am working with that I have simplified a bit:

<select-box name="type" options="$ctrl.ClubTypes"
  track-by="value" label-by="label" required="">
  <div class="SelectBox">
    <div>
      <div></div>
      <input readonly="" class="Input " type="text" value="Club">
    </div>
    <ul class="SelectBox-options ng-scope" ng-if="$ctrl.isShowingOptions" style="">
      <li>Country list items</li> 
    </ul>
  </div>
</select-box>

I am fairly new to using Selenium and programming in general so any help would be fantastic.

Solution

Instead of

new Select(driver.findElement(By.xpath("//select-box[@name='countryCode']"))).selectByVisibleText(country);

Just try

String contry = "IRAN";
    
driver.findElement(By.xpath("//div[@class='SelectBox']")).findElement(By.xpath(".//li[contains(text(),'" + country + "')]"))

Answered By – Hana Bzh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published