【问题标题】:XPath to select specific Dropdown valueXPath 选择特定的下拉值
【发布时间】:2019-01-21 13:59:46
【问题描述】:

我想从下拉列表中选择“VxDev:InterAction 测试自动化列表”。代码如下

<select name="intEmailListId" id="intEmailListId" style="min-width: 210px" data-selected-list="8589934864" class="list_selector">
                <option value="">** Please select a list **</option>

                <option value="">
                    --
                </option>
                <option value="my-contacts">
                    My contacts
                </option>
                <option value="">
                    --
                </option>


                    <option value="8589934952">
                        * 001 New List
                        &nbsp;&nbsp;</option>



                    <option value="8589934880">
                        VxDev: Hard Bounce List (QA team only)
                        &nbsp;&nbsp;</option>


                    <option value="8589934864" selected="">
                        VxDev: InterAction Test Automation List
                        &nbsp;&nbsp;</option>

我在xpath下面试过,以前可以用,现在不行了

try
                    {
                        selectedList =
                            BrowserFactory.Driver.FindElement(
                                By.XPath(".//li[text()[contains(.,'" + listName + "')]]/input"));
                    }
                    catch (NoSuchElementException)
                    {
                        selectedList = BrowserFactory.Driver.FindElement(
                        //By.XPath(".//option[text()[contains(.,'" + listName + "')]]"));
                            By.XPath(".//option[starts-with(normalize-space(text()),'" + listName + "')]"));

                    }

请帮助如何选择具体的下拉值?提前致谢。

【问题讨论】:

  • 也许使用 css 选择器代替....By.Selector("#intEmailListId > option[value='8589934864']")...你需要点击它然后...你可以也使用 SelectElement selectElement = new SelectElement(.By.Selector("#intEmailListId > option[value='8589934864']")) 然后执行 selectElement.SelectByText("drop down visible Text here") 或 selectElement.SelectByValue( “此处选项的值”)或 selectElement.SelectByIndex(此处的选项索引)
  • 你说的“现在不工作”是什么意思?运行代码时会发生什么?
  • 当我运行代码时,脚本正在超时。当我使用上述 xpath 手动检查元素时,Chrome 中没有选择列表元素
  • @Tanya,加selectedList.Click();
  • 请花一点时间来修正 HTML 的缩进。网络上有很多 HTML 美化器可以帮助实现这一点。还要修正代码的缩进,使其更易于阅读。您需要发布完整的(相关的)错误消息。

标签: c# selenium-webdriver


【解决方案1】:

在这里,问题在于您的语法。试试这个:

try{
    selectedList = BrowserFactory.Driver.FindElement(By.XPath("//li[contains(.,'" + listName + "')]/input"));
    By.XPath("//li[contains(.,'" + listName + "')]/input"));
    //By.XPath("//li[contains(text(),'" + listName + "')]/input")); // you can try this as well
   }catch (NoSuchElementException){
          selectedList = BrowserFactory.Driver.FindElement(
          //By.XPath("//option[contains(.,'" + listName + "')]"); // OR
          //By.XPath("//option[contains(text(),'" + listName + "')]");
          By.XPath("//option[starts-with(normalize-space(text())='" + listName + "')]")
        }

【讨论】:

    【解决方案2】:

    你可以试试这个,

    String selectedList = BrowserFactory.Driver.FindElement(By.XPath("//option[@value='8589934864']")).GetAttribute("value");
    

    String selectedList = BrowserFactory.Driver.FindElement(By.XPath("//option[@value='8589934864']")).GetAttribute("innerText");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 2011-07-06
      • 1970-01-01
      相关资源
      最近更新 更多