【问题标题】:How to get xpath of a select tag with option in static dropdown如何在静态下拉列表中使用选项获取选择标签的 xpath
【发布时间】:2021-04-12 14:04:30
【问题描述】:

如何从下面的 html 中获取下拉列表中第二个元素的 xpath 我正在使用带有 specflow 和 selenium 的 C#

在 UI 中,属性类型下有一个静态下拉列表,其中有两个选项 Residential 和 Business 我需要找到第二个选项。

<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>

已经试过了

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

和

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

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: c# selenium xpath css-selectors html-select


    【解决方案1】:

    要选择文本为Business的&lt;option&gt;,您需要为ElementToBeClickable()诱导WebDriverWait,您可以使用Locator Strategies中的任何一个:

    • 使用 CssSelector 和 SelectByValue():

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

      new SelectElement(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("//select[@id='propertyType' and @name='propertyTypr']")))).SelectByText("Business");
      

    【讨论】:

      【解决方案2】:

      如果你只想找到文本是“Business”的选项,你可以试试这个:

      string optionText = "Business";
      
      string xpath = "//option[text()='"+ optionText +"']"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-12
        相关资源
        最近更新 更多