Selenium Java:下拉项会动态更新 [英] Selenium Java : Dropdown items are updated dynamically

查看:39
本文介绍了Selenium Java:下拉项会动态更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何输入将不胜感激:我正在使用的Web应用程序没有选择"功能.标签,下拉列表中的项目会动态更新.意思是当我单击下拉菜单的向下箭头时,它将显示大约10个项目,而当我向下滚动下拉菜单的滚动条"时,将显示10个项目.填充了更多项目.虽然我可以通过在字段"中输入值来选择一个项目,下拉框,然后点击运行时"创建了xpath例如. driver.findElement(By.xpath("//li [@text()='USA'])).click 可以很好地选择任何项目,我需要在其中获取所有项目下降.有没有办法可以实现?

Any inputs will be appreciated: The web application i am working on does not have the "select" tag, and the items in the drop down get updated dynamically. Meaning when i click on the down arrow of the dropdown menu, it would show about 10 items and when i scrolldown the "scrollbar of the dropdown" more items are populated. While i can select an item by typing in the value in the "field" of the dropdown box and by then clicking on the "runtime" created xpath Eg. driver.findElement(By.xpath("//li[@text()='USA']).click which works fine for selecting any item, I would need to get all the items in that drop down. Is there a way this can be achieved ?

推荐答案

driver.findElement(By by) driver.findElements(By by)限制了while DOM

driver.findElement(By by) and driver.findElements(By by) scopes the while DOM.

您可以使用以下方法来定位DOM的一小部分: element.findElements(by by) element.findElements(by by)

you can target a small area of the DOM with: element.findElement(By by) and element.findElements(By by)

使用此:

WebElement dropdown = driver.findElement(By.DROPDOWN LOCATOR);
List<WebElement> options = dropdown.findElements(By.OPTION LOCATOR);

您仍然需要OPTION LOCATOR.但是更容易实现,它只会从parrent元素中收集子元素.

You still need the OPTION LOCATOR. But it is easier to achieve, it will collect child elements from the parrent element only.

选择方法如下:

public void selectByText(List<EebElement> options, String text) {
    for (WebElement element: options) {
        if (element.getText().equals(text)) {
            element.click();
            break;
        }
    }
}

public void selectByValue(List<EebElement> options, String value) {
    for (WebElement element: options) {
        if (element.getAttribute("value").equals(value)) {
            element.click();
            break;
        }
    }
}

这篇关于Selenium Java:下拉项会动态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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