猿问

XPath 选择特定的下拉值

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


                    }

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


神不在的星期二
浏览 474回答 2
2回答

慕哥6287543

你可以试试这个String&nbsp;selectedList&nbsp;=&nbsp;BrowserFactory.Driver.FindElement(By.XPath("//option[@value='8589934864']")).GetAttribute("value");或者String&nbsp;selectedList&nbsp;=&nbsp;BrowserFactory.Driver.FindElement(By.XPath("//option[@value='8589934864']")).GetAttribute("innerText");

大话西游666

在这里,问题在于您的语法。尝试这个:try{&nbsp; &nbsp; selectedList = BrowserFactory.Driver.FindElement(By.XPath("//li[contains(.,'" + listName + "')]/input"));&nbsp; &nbsp; By.XPath("//li[contains(.,'" + listName + "')]/input"));&nbsp; &nbsp; //By.XPath("//li[contains(text(),'" + listName + "')]/input")); // you can try this as well&nbsp; &nbsp;}catch (NoSuchElementException){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedList = BrowserFactory.Driver.FindElement(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //By.XPath("//option[contains(.,'" + listName + "')]"); // OR&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //By.XPath("//option[contains(text(),'" + listName + "')]");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; By.XPath("//option[starts-with(normalize-space(text())='" + listName + "')]")&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答