0

I'm trying to select a value from a dropbox. I have tried with XPath and ID but seem unable to reach it

This is what I tried

var mySelectElm4 = driver.FindElement(By.Id("ddlCountryOfBirth"));
var mySelect4 = new SelectElement(mySelectElm4);
mySelect4.SelectByText("Togo");

And this the html

<div class="form-object">
  <span class="error-star"></span>
  <div class="field-description input-placeholder styled field-description-show">Country of birth</div>
  <div class="field field-select">
    <select name="ctl00$plcMainArea$ddlCountryOfBirth" class="field-data selectCustom watermark hasValue" validate="validate" data-rule-required="true" data-msg-required="This field is required." placeholder="Country of birth" id="ddlCountryOfBirth">
      <option value="AF">Thailand</option>
      <option value="BS">The Bahamas</option>
      <option value="GM">The Gambia</option>
      <option value="TG">Togo</option>
      <option value="TK">Tokelau</option>
      <option value="TO">Zambia</option>
      <option value="ZW">Zimbabwe</option>
    </select>
    <span class="selectCustomBox">
      <span class="selectCustomBoxInner">Afghanistan</span>
    </span>
  </div>
  <div id="ddlCountryOfBirth_e" class="error-message"></div>
  <div class="clearfix"></div>
</div>
Guy
  • 34,831
  • 9
  • 31
  • 66
AutDev
  • 65
  • 8

2 Answers2

0

Try it:

public void SelectIn(By by, string value)
{
    //code here to wait the element be displayed

    //after, the method will select the dropdown with the wanted value
    var dropDownListBox = _driver.FindElement(by);
    var clickThis = new SelectElement(dropDownListBox);
    clickThis.Options.First(o => 
        o.GetAttribute("id") == value || 
        o.Text.Equals(value) || 
        o.GetAttribute("value") == value)
    .Click();
}
Striter Alfa
  • 1,378
  • 1
  • 12
  • 28
0

Try to select by value

var mySelectElm4 = driver.FindElement(By.Id("ddlCountryOfBirth"));
var mySelect4 = new SelectElement(mySelectElm4);
mySelect4.SelectByValue("TG");
Guy
  • 34,831
  • 9
  • 31
  • 66