以下是html的示例。对于所有“下拉列表”,应用程序都是这样的。单击它然后出现选项。我希望能够创建这个方法,并且在一个单独的类中能够调用该方法只能调用它(chooseState(“California”);)并传递 state 变量并选择它国家。
<ul unselectable="on" class="k-list k-reset" tabindex="-1" role="listbox" aria-hidden="true" id="InputModel_State_listbox" aria-live="off" style="overflow: auto;">
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" id="InputModel_State_option_selected" aria-selected="true">--- select ---</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item"/>
<li tabindex="-1" role="option" unselectable="on" class="k-item">statename1</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">statename2</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">statename50</li>
</ul>
以下是我尝试创建的方法示例,它允许您通过状态变量获取状态名称
public void chooseState(String state) {
try {
//This clicks the "Dropdown
driver.findElement(By.cssSelector(State)).click();
//I need the Method to choose by state name. I know I am sort of on the right track because I am able to click the first item I just need to be able to pass any state and it choose the correct one.
**Here are examples of things that failed**
driver.findElement(By.cssSelector(State)).click();
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText(state))).click();
Select select = new Select(driver.findElement(By
.id("InputModel_State_listbox")));
select.deselectAll();
select.selectByVisibleText("state");
Select select = new Select(findElement(by));
select.selectByVisibleText(state); }
} catch (NoSuchElementException e) {
log.error("Fail ", e);
Assert.fail();
}
}
答案 0 :(得分:0)
如果我已正确理解您的问题,您想知道定位器/标识符,以便点击li项目的特定州名称。
您可以使用XPath代替cssSelector(),如下所示。
driver.findElement(By.xPath("//li[text()='" + State + "']")).click();