Java 如何遍历 WebElement 列表并选择一个具有条件的 WebElement?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39735449/
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-08-11 21:25:24  来源:igfitidea点击:

How can I loop through list of WebElements and select one WebElement with a condition?

javaselenium-webdriver

提问by qaNoob

I am using Selenium WebDriver with Java. I am trying to create a method that loops through a list of WebElements to return the first WebElement that contains the text "Ooga Booga", or return null if there are no elements in the list that contains this text.

我在 Java 中使用 Selenium WebDriver。我正在尝试创建一个循环遍历 WebElements 列表的方法,以返回包含文本“Ooga Booga”的第一个 WebElement,或者如果列表中没有包含此文本的元素,则返回 null。

public class myClass {
 private static WebElement element = null;
 private static List<WebElement> elements = null;

 public static WebElement returnOneElement (WebDriver driver){
    elements = driver.findElements(By.cssSelector("someSelector"));
    for (WebElement element : elements){
        String myText = element.findElement(By.cssSelector("classC")).getText();
        if (myText.contains("Ooga Booga")) {
            return element;
        }
    }
    return null;
}

This all works fine until I run into an element that does not have this attribute:

这一切正常,直到我遇到一个没有此属性的元素:

String myText = element.findElement(By.cssSelector("classC")).getText();

How can I continue to loop through my List while ignoring any elements that do not have "someDiv" inside of it?

如何在忽略其中没有“someDiv”的任何元素的同时继续遍历我的列表?

<div class="someSelector">
    <div class="classA">
    <div class="classB">
</div>

<div class="someSelector">
    <div class="classA">
    <div class="classB">
    <div class="classC">
</div>

I need to search for text inside div "classC".

我需要在div“classC”中搜索文本。

采纳答案by MasterJoe2

Use findElement-S method as shown below.

使用 findElement-S 方法,如下所示。

    for (WebElement element : elements) {
        List<WebElement> mightHaveSomeDiv = element.findElements(By.cssSelector("someDiv"));

        if (mightHaveSomeDiv.size() > 0) {
            //Can iterate the list if you expect more than one div of type someDiv.
            String myText = mightHaveSomeDiv.get(0).getText();
            if (myText.contains("Ooga Booga")) {
                return element;
            }
        }
    }

回答by robx

Btw, I would recommend to try and break away from static methods like this. It will make your life a living hell later on because of it being static. Everything using it starts to want static.

顺便说一句,我建议尝试摆脱这样的静态方法。因为它是静态的,它以后会让你的生活变成一个活生生的地狱。使用它的一切都开始想要静态。

Anyway, lets break it down. This cssSelector will allow you to grab the child divs by the tagname instead of classname.

无论如何,让我们分解它。这个 cssSelector 将允许您通过标记名而不是类名来获取子 div。

By.cssSelector(".someSelector div")

Now that we have a list of WebElement, which are all div elements, lets just look for the text in the div. No need to worry about class attribute

现在我们有一个 WebElement 列表,它们都是 div 元素,让我们在 div 中查找文本。无需担心类属性

public static WebElement returnOneElement (WebDriver driver){
            elements = driver.findElements(By.cssSelector(".someSelector div"));
            for (WebElement element : elements){
                if(element.getText().contains("Ooga Booga")) {
                    return element;
                }
            }    
       return null;
    }

Alternatively, you may find the target directly with xpath locators such as

或者,您可以直接使用 xpath 定位器找到目标,例如

//div[@class='someSelector']//div[contains(.,'Ooga Booga')]

Be warned though, if you have multiple div that contains the text "Ooga Booga", you will get multiple WebElements, and what you do with it next may not be what you expect. Actually, this warning should also apply to the loop. It will break out of your first div element that contains "Ooga Booga". You will have to adjust the logic if you want exact match.

但是请注意,如果您有多个包含文本“Ooga Booga”的 div,您将获得多个 WebElements,接下来您用它做什么可能不是您所期望的。实际上,这个警告也应该适用于循环。它会跳出包含“Ooga Booga”的第一个 div 元素。如果您想要完全匹配,则必须调整逻辑。

回答by Ankush kumar

Here is your answer.. Firstly Declare a parent web locator

这是你的答案.. 首先声明一个父网络定位器

WebElement element1 = driver.findElement(By.id("##abc##"));

Now in case you want to perform any action or anything within the locator defined above then you can do it as follows:

现在,如果您想在上面定义的定位器中执行任何操作或任何操作,则可以按如下方式进行:

element1.findElement(By.xpath("  .        //##yourFurtherLocABC")).click();

element1.findElement(By.xpath("  .      //##yourFurtherLocXYZ")).getText();

Important= Do remember to keep a .at the beginning of any child elements!

重要= 请记住.在任何子元素的开头保留一个!