1

How to get the xpath for the second element in the dropdown from below html I am using C# with specflow and selenium

In the UI there is a static dropdown under the property type and within it have two options Residential and Business I need to find the second one.

<select class= "form-control ng-pristine ng-empty ng-invalid-required ng-touched" type="text" 
id="propertyType" name="propertyTypr" ng-modal="lead.currentLeadContact.IsResidential" required 
style>
<option value="? object:null ?"></option>
<option value="true">Residintial</option>
<option value="false">Business</option>
</select>

Already tried with

//select[@id='prorpertyType']/option[@value='false' and . ='Business']

and

//select[@id='propertyType' and @value='1'] same xpath with value 0 and 2

I appreciate if anyone could help me with that.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sultana
  • 11
  • 3

2 Answers2

0

To select the <option> with text as Business you need to induce WebDriverWait for the ElementToBeClickable() and you can use either of the Locator Strategies:

  • Using CssSelector and SelectByValue():

    new SelectElement(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("select#propertyType[name='propertyTypr']")))).SelectByValue("false");
    
  • Using XPath and SelectByText():

    new SelectElement(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("//select[@id='propertyType' and @name='propertyTypr']")))).SelectByText("Business");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

If you just want to find the option that have the text is "Business", you can try this:

string optionText = "Business";

string xpath = "//option[text()='"+ optionText +"']"
W332
  • 11
  • 1