Selenium Webdriver w/Java:使用一个命令定位具有多个类名的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16089492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:52:50  来源:igfitidea点击:

Selenium Webdriver w/Java: locating elements with multiple class names with one command

javaselenium-webdriver

提问by Will R.

I'm trying to use Selenium (2.31.0, using JavaSE 1.6 and IE9) to find a series of elements on a page. These elements all have one of two class names, 'dataLabel' or 'dataLabelWide'. Currently, my code collects these elements in two separate ArrayLists, one for each class name, then converts them to arrays and combines them into one array. This method, however, lists the elements out of order, and I need them to be left in the same order as they're found in the page's HTML source.

我正在尝试使用 Selenium(2.31.0,使用 JavaSE 1.6 和 IE9)在页面上查找一系列元素。这些元素都具有两个类名之一,“dataLabel”或“dataLabelWide”。目前,我的代码将这些元素收集在两个单独的 ArrayList 中,每个类名一个,然后将它们转换为数组并将它们组合成一个数组。但是,此方法不按顺序列出元素,我需要将它们保留在与在页面的 HTML 源代码中找到的顺序相同的顺序。

The above-mentioned section of my code looks like this (with comments added for explanation):

我的代码的上述部分如下所示(添加了注释以进行解释):

// Application runs on WebDriver d, an InternetExplorerDriver.
// After navigating to the page in question...

List<WebElement> labels = d.findElements(By.className("dataLabel"));
List<WebElement> wLabels = d.findElements(By.className("dataLabelWide"));
// Locates the elements of either type by their respective class name.

WebElement[] labelsArray = labels.toArray(new WebElement[labels.size()]);
WebElement[] wLabelsArray = wLabels.toArray(new WebElement[wLabels.size()]);
// Converts each ArrayList to an array.

List<WebElement> allLabels = new ArrayList<WebElement>();
// Creates an ArrayList to hold all elements from both arrays.

for(int a = 0; a < labelsArray.length; a++) {
    allLabels.add(labelsArray[a]);
}
for(int b = 0; b < wLabelsArray.length; b++) {
    allLabels.add(wLabelsArray[b]);
}
// Adds elements of both arrays to unified ArrayList, one by one.

WebElement[] allLabelsArray = allLabels.toArray(new WebElement[allLabels.size()]);
// Finally converts unified ArrayList into array usable for test purposes.
// Far too complicated (obviously), and elements end up out-of-order in array.

I think the most efficient solution would be to locate elements with either class name so they'd be included in a single list/array right away. I've done some searching on my own, but I haven't found any conclusive ideas on how I might manage this task. If there is some way to do this, please tell me about it.

我认为最有效的解决方案是使用任一类名定位元素,以便将它们立即包含在单个列表/数组中。我自己进行了一些搜索,但没有找到任何关于如何管理这项任务的结论性想法。如果有什么方法可以做到这一点,请告诉我。

回答by JimEvans

Why wouldn't you do the following:

你为什么不做以下事情:

driver.findElement(By.cssSelector(".dataLabel,.dataLabelWide");

The '.' selector says, "give me all elements with this class." The ',' operator is the CSS selector 'or' operator.

这 '。' 选择器说,“给我这个类的所有元素。” ',' 运算符是 CSS 选择器的 'or' 运算符。