Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

c# - How to Select an Item in a Dropdown List

I am not able to click a dropdown list to open and then select a particular element in the list in an web app.

I have tried the following C# code:

// Locate and click the Dropdown list
var primaryPhoneDD = Driver.driver.FindElement(By.XPath(linkLocator.xPathLocatorRROSignUp[29]));
primaryPhoneDD.Click();

// Click an item from the dropdown list
 var selectAnItem = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath(linkLocator.xPathLocatorRROSignUp[37])));

selectAnItem.Click();

Also tried,

SelectElement selectPrimaryPhoneDD = new SelectElement(primaryPhoneDD);
var selectPrimaryPhoneDD = new SelectElement(primaryPhoneDD);
selectPrimaryPhoneDD.SelectByIndex(1);

xpath used to locate the dropdown list:

//div[starts-with(@class, 'hcf-primaryPhoneDropdownMainDiv')]/ul[@class='hcf-primary-dropdown-wrapper']

Here is the html code:

<div class="hcf-primaryPhoneMainContainerDiv">
<div class="hcf-primaryPhoneNoMainContainerDiv hcf-PhoneNoMainContainerDiv">
<div class="hcf-primaryPhoneLabel hcf-PhoneLabel">Primary Phone:</div>
<div class="hcf-primaryPhoneNoContainer hcf-PhoneNoContainer">
<div class="hcf-primaryPhoneDropdownMainDiv hcf-PhoneDropdownMainDiv col-md-3 col-sm-3">
<div id="ctl00_ctl62_g_6dac061a_e162_422c_bd9c_6ea80ab6bf1f_PrimaryDropDownSelectedDiv" class="hcf- 
primaryDropDownSelectedDiv hcf-DropDownSelectedDiv"><span 
id="ctl00_ctl62_g_6dac061a_e162_422c_bd9c_6ea80ab6bf1f_PrimaryDropDownSelectedSpan" class="hcf- 
primaryDropDownSelectedSpan hcf-DropDownSelectedSpan">Type</span><img class="hcf-arrowDownImg" 
src="/_layouts/15/styles/themable/url/images/svgs/CovidDropdownArrowImg.svg"></div>

<ul class="hcf-primary-dropdown-wrapper" style="display: none;">
<li class="hcf-primary-phoneType-Select-option"><label class="hcf-primary-phoneType-Select- 
optionLabel">Type</label><input type="hidden" class="hrf-hidden-primaryPhone-type" value="0"></li>
  <li class="hcf-primary-phoneType-Select-option"><label class="hcf-primary-phoneType-Select- 
  optionLabel">Landline</label><input type="hidden" class="hrf-hidden-primaryPhone-type" value="1"> 
  </li>

  <li class="hcf-primary-phoneType-Select-option"><label class="hcf-primary-phoneType-Select- 
  optionLabel">Mobile</label><input type="hidden" class="hrf-hidden-primaryPhone-type" value="2"> 
  </li>

  <li class="hcf-primary-phoneType-Select-option"><label class="hcf-primary-phoneType-Select- 
  optionLabel">Work</label><input type="hidden" class="hrf-hidden-primaryPhone-type" value="3"></li>
  </ul></div>

  <div id="ctl00_ctl62_g_6dac061a_e162_422c_bd9c_6ea80ab6bf1f_PrimaryPhoneInputContainer" class="hcf- 
  primaryPhoneInputContainer hcf-PhoneInputContainer col-md-7 col-sm-7">
  <div class="hcf-PrimaryphoneContainerHtml"><input name="PrimaryTelephoneType" type="text" 
  maxlength="3" id="PrimaryTelephone" class="hcf-input-box-phone hcf-PrimaryareaCode hcf- 
  PrimaryPhoneType" role="textbox" required="required" aria-required="true" data-label="Please enter 
  a valid phone number" value="">

  <input name="PrimaryTelephoneType" type="text" maxlength="3" id="PrimaryTelephone" class="hcf- 
  input-box-phone hcf-PrimaryexchangeCode hcf-PrimaryPhoneType" role="textbox" required="required" 
  aria-required="true" data-label="Please enter a valid phone number" value="">

  <input name="PrimaryTelephoneType" type="text" maxlength="4" id="PrimaryTelephone" class="hcf- 
  input-box-phone hcf-PrimarylineCode hcf-last hcf-PrimaryPhoneType" role="textbox" 
   required="required" aria-required="true" data-label="Please enter a valid phone number" value=""> 
   </div></div></div></div></div>

Hoping get any feedback. Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...