如何使用 Xpath Selenium WebDriver JAVA 获取多个元素(div)?

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

How to get multiple elements (div) using Xpath Selenium WebDriver JAVA?

javaseleniumxpatharraylistselenium-webdriver

提问by Vinicius Kostriuba

I'm having some difficulties to get multiple elements from a div. As you can see, I got the div[1] and after that I want to get more 8 div inside div[1]. But I want to do that with xpath(same line). Anyone have some idea?

我在从 div 中获取多个元素时遇到了一些困难。如您所见,我得到了 div[1],之后我想在 div[1] 中获得更多 8 个 div。但我想用 xpath(同一行)来做到这一点。有人有什么想法吗?

Father = .//*[@id='result']/div[1]

父亲 = .//*[@id='result']/div[1]

Sons =

儿子 =

          .//*[@id='result']/div[1]/div[6]
          .//*[@id='result']/div[1]/div[9]
          .//*[@id='result']/div[1]/div[12]
          .//*[@id='result']/div[1]/div[15]
          .//*[@id='result']/div[1]/div[18]
          .//*[@id='result']/div[1]/div[21]
          .//*[@id='result']/div[1]/div[24]
          .//*[@id='result']/div[1]/div[27]

My Code:

我的代码:

List<String> productName = new ArrayList<String>();
List<WebElement> allProductsName = driver.findElements(
        By.xpath(".//*[@id='result']/div[1]/div[6]"));
for(WebElement w : allProductsName) {
    productName.add(w.getText());
}
System.out.print(productName);

I imagined something like that ".//*[@id='result']/div[1]/div[6],[9]..."or

我想象过这样的事情".//*[@id='result']/div[1]/div[6],[9]..."

".//*[@id='result']/div[1]/div[6 and 9 ....]"

But didn't work.

但没有奏效。

回答by Poojan Dave

as your product description comes in 6,9,12,15,18 use this code that will iterate through 6 to 27 in multiplication of 3.

当您的产品描述出现在 6、9、12、15、18 时,请使用此代码,它将以 3 的乘法迭代 6 到 27。

List<String> productName = new ArrayList<String>();


for(int i=6; i<=27; i=i+3)
{

    List<WebElement> allProductsName = driver.findElements(By.xpath(".//*[@id='result']/div[1]/div["+i+"]"));
    for(WebElement w : allProductsName) {
        productName.add(w.getText());
    }
}
System.out.print(productName);

回答by Grasshopper

Just remove the index number at the end of the xpath and get all the immediate child divs in a List.

只需删除 xpath 末尾的索引号并获取列表中的所有直接子 div。

    List<String> productName = new ArrayList<String>();
               List<WebElement> allProductsName = driver.findElements(
By.xpath(".//*[@id='result']/div[1]/div"));
               for(WebElement w : allProductsName) {
                   productName.add(w.getText());
               }

        System.out.print(productName);

回答by robx

The first thing I see is that the development of the application should not have duplicate static ID. If used like this, should be a class instead.

我看到的第一件事是应用程序的开发不应该有重复的静态ID。如果这样使用,应该是一个类。

Second thing is, the xpath expression is not good approach. Try to never use //* , and you don't need the dot (.//), as it can take much longer to find the element(s), and make tests slower.

第二件事是,xpath 表达式不是一个好方法。尽量不要使用 //* ,并且您不需要点 (.//),因为查找元素可能需要更长的时间,并使测试变慢。

You don't show any html code, but based on your sample you should be able to get all div element using

您没有显示任何 html 代码,但根据您的示例,您应该能够使用

List<WebElement> results = driver.getElements(By.id("result"));

or if you insist on xpath

或者如果你坚持使用 xpath

List<WebElement> results = driver.getElements(By.xpath("//div[@id='result']"));