java - 带有 Selenium Java 的剑道组合框

标签 java selenium kendo-ui

我正在尝试使用 selenium 自动化 Kendro UI 应用程序。我不确定 selenium 是否是用于 Kendro UI 应用程序的正确工具。

您能帮我用 selenium 选择 Kendro UI Combobox 下拉值吗?

我尝试了多种方法,但到目前为止都没有效果。

我的代码: 我单击下拉列表中的箭头并等待几秒钟,然后尝试选择一个值。 但问题是,由于某种原因,下拉菜单十次中有四次有效。所以这不是一个正确的解决方案

WebElement firstDropDown = driver.findElement(By.xpath("//innova-combobox-input[@name='LHSCID']//span[@class='k-select']"));

firstDropDown.click();
new WebDriverWait(driver,50).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'LHSC 1000 Technician Site A')]")))

driver.findElement(By.xpath("//span[contains(text(),'LHSC 1000 Technician Site A')]")).isDisplayed();
driver.findElement(By.xpath("//span[contains(text(),'LHSC 1000 Technician Site A')]")).click();

下面是下拉菜单页面上的 HTML 代码:

<input name="{{::$ctrl.name}}_input" class="k-input innova-invalid" type="text" autocomplete="off" title="" role="combobox" aria-expanded="false" style="" tabindex="0" aria-disabled="false" aria-autocomplete="list" aria-owns="" aria-busy="false" aria-activedescendant="d5147cf1-35ff-4160-ad0f-c164916f59c7" xpath="1">

和下拉值

<ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" aria-live="polite" data-role="staticlist" role="listbox" style="" xpath="1">
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-focused" data-offset-index="0" id="d5147cf1-35ff-4160-ad0f-c164916f59c7" style=""></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">
<span ng-bind="dataItem.Display"></span>
</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="2" style=""></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="3"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="4"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="5"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="6"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="7"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="8"></li>
</ul>

最佳答案

你是对的。由于这是一个复杂的应用程序,我们可以尝试采用多种方法来解决此问题。

相当简单的尝试就是清除并发送 key 。

WebElement firstDropDown = driver.findElement(By.xpath("(//*[text() = 'T-shirt Fabric']//following::input)[1]"));
firstDropDown.click();
firstDropDown.clear();
firstDropDown.sendKeys("Cotton");

如果出现 StaleElementException,请尝试在每一行中使用 findElement。

driver.findElement(By.xpath("(//*[text() = 'T-shirt Fabric']//following::input)[1]")).click();
driver.findElement(By.xpath("(//*[text() = 'T-shirt Fabric']//following::input)[1]")).clear();
driver.findElement(By.xpath("(//*[text() = 'T-shirt Fabric']//following::input)[1]")).sendKeys("Cotton");

如果此选项在您的应用程序中不起作用,请尝试使用 Actions 类。

Actions action = new Actions(driver);
WebElement firstDropDown = driver.findElement(By.xpath("(//*[text() = 'T-shirt Fabric']//following::input)[1]"));
action.moveToElement(firstDropDown).click().build().perform(); 
action.sendKeys(Keys.ARROW_RIGHT).sendKeys(Keys.BACK_SPACE).sendKeys(Keys.BACK_SPACE).release().build().perform();
action.sendKeys("Cotton").build().perform();
action.sendKeys(Keys.TAB).build().perform();

您可能可以将其放入一个方法中,并为每个下拉列表重复使用它,如下所示。

关于java - 带有 Selenium Java 的剑道组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58630364/

相关文章:

jquery - KENDO UI - 尝试在运行时添加 div

javascript - 如何从 Kendo View 模型获取 DOM 元素?

java - 为什么我在 Java 中收到 Unreachable Statement 错误?

python - Chromedriver 最大化问题

java - 使用 Selenium 和 Java,我需要一种比 "CSSSelector"更好的方法来选择单选按钮,因为不同单选组中具有相同名称的值

python - Selenium pdf自动下载不起作用

java - 如何在不创建循环的情况下向有向无环图添加边

java - Guava Multimaps 通配符 putAll

java - 用于关闭 FileWriter 的哨兵值 (Java)

javascript - Kendo Scheduler 中的英语时间格式