gpt4 book ai didi

java - findElement 在 for-each 循环中未按预期工作

转载 作者:行者123 更新时间:2023-12-02 08:50:11 24 4
gpt4 key购买 nike

我完全陷入困境,不知道为什么我会得到这样的输出。这是相关的片段。

WebElement section = driver.findElement(By.xpath("//div[contains(@class,'_main')]"));
List<WebElement> searchResults = new ArrayList<WebElement>();
searchResults.addAll(section.findElements(By.xpath("//div[contains(@class,'_listing')]")));
for (WebElement element : searchResults) {
System.out.println(element.getAttribute("innerHTML"));
System.out.println(element.findElement(By.xpath("//p[contains(@class,'_2tux')]")).getText());

currentlistings.add(newListing('f',element.findElement(By.xpath("//a[contains(@class,'_1oem')]")).getAttribute("href"),
element.findElement(By.xpath("//p[contains(@class,'_2tux')]")).getText(),
element.findElement(By.xpath("//div[contains(@class,'_f3l _4x3g')]")).getText()));
}

将元素存储到列表时,代码似乎运行良好,大小正确,并且当我按预期迭代列表元素时,innerHTML 输出。

然而,当我尝试在每个迭代元素上使用 findElement 时,它似乎从索引 0 处的元素读取 HTML,即使之前的命令从正确的索引输出 html。任何指示将不胜感激。

最佳答案

您需要使用.//

element.findElement(By.xpath(".//p[contains(@class,'_2tux')]")).getText(),
element.findElement(By.xpath(".//div[contains(@class,'_f3l _4x3g')]")).getText()));

关于java - findElement 在 for-each 循环中未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60834053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com