XPath选择特定的Dropdown值

时间:2018-08-14 16:33:55

标签: c# selenium-webdriver

我想从下拉列表中选择“ 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 + "')]"));

                    }

请帮助如何选择特定的下拉值?预先感谢。

2 个答案:

答案 0 :(得分:0)

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

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 + "')]")
        }

答案 1 :(得分:0)

您可以尝试

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

OR

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