0

am trying to select country code from dropdown where the elements has no index or ID i can only select by value, i tried using SelectByValue & VisibleText Both didnt work also tried to list element and loop on them but didnt work either

Update: it gives me error: org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been “select” but was “button”

how can i select from button with list of elements ??

here is the code:

public void selectInDropDownMenuCountryCode(final WebDriver driver, final By selector, final String selection) {
    _wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
    final Select select = new Select(driver.findElement(selector));
    //select.selectByVisibleText(selection);
    //select.selectByValue(selection);
     String code;

      List<WebElement> optionsD = select.getOptions();

    for (WebElement option : optionsD) {
         code = option.getAttribute("value");
        if (code == selection) {
            option.click();
            break;
        }       

    }
}

Screenshot of html here is the html code

Bista
  • 7,869
  • 3
  • 27
  • 55
  • Show exact selectors you used – Andersson Dec 28 '16 at 09:35
  • 2
    Possible duplicate of [How to select a dropdown value in Selenium WebDriver using Java](http://stackoverflow.com/questions/20138761/how-to-select-a-dropdown-value-in-selenium-webdriver-using-java) – BackSlash Dec 28 '16 at 09:37

2 Answers2

0

If you refer the HTML you can see the Dropdown isn't built using HTML Select tag. So you can't use the below methods:-

  1. selectByValue
  2. selectByIndex
  3. selectByVisibleText

You have to identify the elements using regular methods to handle dropdown. e.g: Try clicking on dropdown by identifying the element and then again click on the country name to select the value.

Paras
  • 3,197
  • 2
  • 20
  • 30
0

Try to use simple click() method to select required drop-down option:

_wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(), 'Egypt (+20)')]"))).click();

XPath like "//a[@country='Egypt']" or "//a[@value='EG']" seem to be applicable also

Note that you should click on appropriate element to open drop-down before clicking on its option

Andersson
  • 51,635
  • 17
  • 77
  • 129