如何使用Selenium WebDriver和Java从Kendo下拉菜单中选择一个选项 [英] How to select an option from the kendo dropdown using selenium webdriver and Java

查看:84
本文介绍了如何使用Selenium WebDriver和Java从Kendo下拉菜单中选择一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML代码:

Here is my HTML code:

<ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" id="ddlSettleMode_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="18e2d509-b1e1-4588-bd2a-dcff29b45b83">Select</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">Cash</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="2">Card</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="3">Cheque</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="4">Paytm</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="5">NEFT</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="6">DD</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="7">IMPS</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="8">Online</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="9">UPI</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="10">Digital</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="11">CMS</li><li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="12">Univ</li></ul>

这是我的硒代码:

WebDriverWait Waita = new WebDriverWait(driver, 20);
        Waita.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text() = 'Select']")));
        Waita.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text() = 'Select']"))).click();
        WebDriverWait waitb = new WebDriverWait(driver, 20);
        waitb.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[text() = 'Cash']")));
        waitb.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text() = 'Cash']"))).click();

我收到以下错误消息:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //li[text() = 'Cash'] (tried for 20 second(s) with 500 milliseconds interval)

推荐答案

要使用定位器策略:

To select the item with text as Cash within the kendo dropdown using Selenium instead of using `` you you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.k-list.k-reset#ddlSettleMode_listbox li.k-item[data-offset-index='0']"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.k-item[data-offset-index='1']"))).click();

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='k-list k-reset' and @id='ddlSettleMode_listbox']//li[contains(@class, 'k-item') and text()='Select'][@data-offset-index='0']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='k-item' and text()='Cash'][@data-offset-index='1']"))).click();
    

  • 这篇关于如何使用Selenium WebDriver和Java从Kendo下拉菜单中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆