我在C#中使用Selenium chromedriver。我有没有选择标签的下拉列表。
那么,如何选择值呢?我设法选择下拉菜单并单击它。但是我不知道如何检索值并选择它。
感谢您的帮助。谢谢。
用于单击下拉列表的代码:
IWebElement dropdown_priority = driver.FindElement(By.XPath(".//div[@class='editor-col'][2]//div[@class='editor-field'][2]//child::span")); //select priority dropdown
dropdown_priority.Click();
元素:
<div class="editor-field">
<span title="" style="" class="k-widget k-dropdown k-header" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="PriorityId_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false" aria-activedescendant="98260767-707a-4bcc-a1b7-b44d4eeccf90">
<span unselectable="on" class="k-dropdown-wrap k-state-default">
<span unselectable="on" class="k-input">- Select -</span><span unselectable="on" class="k-select">
<span unselectable="on" class="k-icon k-i-arrow-s">select</span>
</span>
</span>
<input id="PriorityId" name="PriorityId" type="text" data-role="dropdownlist" style="display: none;" data-bind="value:PriorityId" aria-invalid="true" class="k-invalid">
</span>
<div class="k-widget k-tooltip k-tooltip-validation k-invalid-msg field-validation-error" style="margin: 0.5em; display: block;" data-for="PriorityId" data-valmsg-for="PriorityId" id="PriorityId_validationMessage" role="alert">
<span class="k-icon k-warning"></span>
The Issue Priority field is required.
<div class="k-callout k-callout-n"></div>
</div>
</div>
首先,您需要在Nuget Package Manager上获取Selenium.Support包
然后你可以使用此方法选择你想要的元素
new SelectElement(driver.FindElement(By.XPath(".//div[@class='editor-col'][2]//div[@class='editor-field'][2]//child::span"))).SelectByIndex(1);
new SelectElement(driver.FindElement(By.XPath(".//div[@class='editor-col'][2]//div[@class='editor-field'][2]//child::span"))).SelectByText("YourSelection");
new SelectElement(driver.FindElement(By.XPath(".//div[@class='editor-col'][2]//div[@class='editor-field'][2]//child::span"))).SelectByValue("YourValue");
我找到了如下解决方案。
我有带有选项的下拉列表(未指定,低,中,高(。使用以下代码管理选择"中值"。
/*Create and initialize object*/
IWebElement dropdown_priority = driver.FindElement(By.XPath(".//div[@class='editor-col'][2]//div[@class='editor-field'][2]//child::span//span[contains(@class, 'k-dropdown-wrap')]")); //select priority dropdown
dropdown_priority.Click(); // click priority dropdown, the dropdown selection will be displayed
Thread.Sleep(500);
/*Create and initialize object*/
IWebElement optionsLists = driver.FindElement(By.XPath(".//*[@id='PriorityId_listbox']//li[3]")); //select value on index 3
optionsLists.Click(); //click selected value