我是 Selenium 网络驱动程序的初学者。我无法选择剑道下拉列表
这是我的代码:
Select profcat = new Select (driver.findElement(By.xpath("/html/body/div[1]/div[2]/section[2]/section/div/div[2]/div/div/form/div/div/div[1]/div/div[1]/div[4]/div/div/div/span/span/span[1]")));
profcat.selectByIndex(2);
<div unselectable="on" style="overflow: auto; position: relative; height: auto;">
<ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" id="ddlCategoryTy_listbox" aria-live="polite" data-role="staticlist" role="listbox">
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" data-offset-index="0" id="f3776f55-02cc-45e9-9dd1-b32f24d457bd">Select</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">Room</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="2">Health Club</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="3">Laundry</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="4">Recreational Activities</li>
</ul>
</div>
最佳答案
要从下拉列表中选择一个项目,您需要编写一个 XPath 来查询您要选择的元素的文本。我还会在您希望单击的选项上调用 WebDriverWait
,以考虑下拉选项的任何延迟加载时间:
// expand the dropdown -- click on the div, may need to click something else
driver.findElement(By.xpath("//div[ul[@id='ddlCategoryTy_listbox']]")).click()
// wait on an option to exist
WebDriverWait wait = new WebDriverWait(driver, 10);
optionToClick = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Health Club']")));
// click the option
optionToClick.click();
您需要导入 org.openqa.selenium.support.ui.ExpectedConditions
和 org.openqa.selenium.support.ui.WebDriverWait
才能正常工作。
上述代码单击 div
元素展开下拉列表,然后使用查询 li
文本的 XPath 选择选项“Health Club”。您可能需要更新用于展开下拉列表的选择器 - 根据您提供的 HTML,我无法判断哪个元素用于打开下拉列表以显示选项。
关于java - 如何在 Selenium Web 驱动程序中选择 Kendo 下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59487213/