使用selenium WebDriver查找元素

问题描述 投票:0回答:2

我试图在列表中使用查找元素,但我收到错误:

org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“select”但是“li”

我要找的元素在列表中:

<span unselectable="on" class="k-input">[SELECT2]</span>

该元素位于:

<li tabindex="-1" role="option" unselectable="on" class="k-item" data-index="2">ARKANSAS</li>

我试图使用以下java代码找到它:

driver.findElement(By.cssSelector("span.k-input")).click(); Thread.sleep(1000); //it works
new Select(driver.findElement(By.xpath("//*[@id=\"DriverLicState_listbox\"]/li[3]"))); // error

我试图在列表中选择一个元素但它似乎也不起作用。我该怎么做呢?

java selenium webdriver
2个回答
1
投票

Select类旨在处理“select”元素。您显示的一小部分HTML代码表明这是一种自定义选择控件,构建为级联列表。您可能必须编写自己的selenium代码来与它进行交互...具有适当方法的CustomSelect类。


0
投票

这里有两种解决这个问题的方法:

我使用Selenium IDE查看更多目标选项。

选项1:xpath:idRelative

driver.findElement(By.xpath("//*[@id=\"DriverLicState_listbox\"]/li[3]")).click();

选项2:xpath:位置

driver.findElement(By.xpath("//body/div[2]/div/ul/li[3]")).click();
© www.soinside.com 2019 - 2024. All rights reserved.