Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XPath来选择特定的下拉值_C#_Selenium Webdriver - Fatal编程技术网

C# XPath来选择特定的下拉值

C# XPath来选择特定的下拉值,c#,selenium-webdriver,C#,Selenium Webdriver,我想从下拉列表中选择“VxDev:交互测试自动化列表”。代码如下所示 <select name="intEmailListId" id="intEmailListId" style="min-width: 210px" data-selected-list="8589934864" class="list_selector"> <option value="">** Please select a list **</option>

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

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

请帮助如何选择特定的下拉列表值?提前感谢。

这里的问题是您的语法。试试这个:

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 + "')]")
        }
你可以试试这个

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


可能会改用css选择器…By.SelectorintEmailListId>option[value='8589934864']…您需要单击它,然后…您还可以使用SelectElement SelectElement=new SelectElement.By.SelectorintEmailListId>option[value='8589934864']然后执行selectElement.SelectByTextdrop down visible Text here或selectElement.SelectByValue here选项的值或selectElement.SelectByIndex here选项的索引您所说的现在不工作是什么意思?当你运行代码时会发生什么?当我运行代码时,脚本会得到timedout。当我使用上述xpath手动检查元素时,列表元素没有被选中Chrome@Tanya,只需添加所选列表。单击;请花一分钟来修复HTML的缩进。网络上有很多HTML美化工具可以帮助实现这一点。还要修正代码的缩进,以便更容易阅读。您需要发布完整的相关错误消息。
String selectedList = BrowserFactory.Driver.FindElement(By.XPath("//option[@value='8589934864']")).GetAttribute("value");
String selectedList = BrowserFactory.Driver.FindElement(By.XPath("//option[@value='8589934864']")).GetAttribute("innerText");