【问题标题】:How to select Kendo dropdown in selenium web driver?如何在硒网络驱动程序中选择剑道下拉菜单?
【发布时间】:2019-12-26 10:39:31
【问题描述】:

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

【问题讨论】:

  • 用相关的 HTML 更新问题

标签: java selenium selenium-webdriver


【解决方案1】:

要从下拉列表中选择一个项目,您需要编写一个 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.ExpectedConditionsorg.openqa.selenium.support.ui.WebDriverWait 才能使其正常工作。

以上代码单击div 元素以展开下拉列表,然后使用查询li 文本的XPath 选择选项“健康俱乐部”。您可能需要更新用于展开下拉列表的选择器 - 根据您提供的 HTML,我无法判断哪个元素用于打开下拉列表以显示选项。

【讨论】:

  • @MurugesanK 很高兴看到它对您有用。随意将此帖子标记为您的问题的答案,以便其他用户可以看到它有帮助。
【解决方案2】:

Kendo UI 没有通常的下拉菜单。因此,您的代码将无法使用它。您需要使用下一个算法:

  1. 点击按钮打开下拉菜单。
  2. 等待元素出现,其中包含下拉列表中的值列表(我在 chrome 中使用调试来获取此元素)。
  3. 在出现的值列表中进行迭代,然后单击您需要的值。

我正在使用这种算法来处理这种下拉菜单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2017-11-17
    • 2016-08-08
    • 1970-01-01
    相关资源
    最近更新 更多