Selenium WebDriver 在 Web 元素内查找 Web 元素

huangapple go评论72阅读模式
英文:

Selenium Webdriver Find Web Element Within Web Element

问题

我有一些代码,它能够找到所有具有特定XPath的元素,我们称之为FB帖子。

然后我通过一个for循环对它们进行遍历。假设有20个这样的元素。

对于这20个元素中的每一个。它们内部都有另一个元素,我们称之为“赞”按钮。

因此,当我迭代遍历FB帖子时,我应该能够选择任何一个FB帖子元素。然后在其中搜索“赞”按钮。通过它的XPath

// 获取FB帖子
List<WebElement> fbPostsList = driver.findElements(By.xpath("//div[contains(@data-pagelet,'FeedUnit')]"));	


// 循环遍历所有可见的FB帖子
for (WebElement element : fbPostsList) {
	
	// 基于FB帖子元素查找“赞”按钮
	List<WebElement> likeLocations = element.findElements(By.xpath("//div[@aria-label='Like']"));
		
	// 遍历元素
	for (WebElement likeLocationsElement : likeLocations) {

		System.out.println(likeLocationsElement.getLocation());


	}

}

上面的代码应该正好做到这一点。然而,我发现第二个“findElements”用于寻找“赞”按钮。实际上列出了每个“赞”按钮。假设窗口中有20个FB帖子。它将列出20个赞按钮。

因为我在第一个for循环内选择了单个FB帖子元素。然后进行“findElements”操作,我应该只在特定的元素内部进行搜索,或者说是特定的FB帖子内部?

英文:

I have some code that finds all the elements, let's call them FB posts, that have a certain XPath.

Then I loop through these in a for-loop. Say there are 20 of these.

For each of these 20 elements. They have another element, let's call it a like button, inside of them.

So as I iterate through the FB posts. I should be able to select any FB post element. Then search for the Like button inside it. Via its xPath

// Get FB posts
List<WebElement> fbPostsList = driver.findElements(By.xpath("//div[contains(@data-pagelet,'FeedUnit')]"));	


// Loop through all FB posts we can see
for (WebElement element : fbPostsList) {
	
	// Find like button based on FB post element
	List<WebElement> likeLocations = element.findElements(By.xpath("//div[@aria-label='Like']"));
		
	// Loop through elements
	for (WebElement likeLocationsElement : likeLocations) {

		System.out.println(likeLocationsElement.getLocation());


	}

}

The code aboove should do exactly that. However, what I'm finding is the second "findElements" for the like button. Is basically listing every single like button. So say there were 20 FB posts in the window. It will list 20 likes buttons.

Since I am selecting a single FB post element. Inside the first for-loop. Then doing a "findElements", surly I should only be searching inside that specific element, or FB post?

答案1

得分: 2

你的问题出在 XPath 表达式上。Java 文档 对此有清晰的描述:

> 当使用 XPath 时,请注意 WebDriver 遵循标准惯例:以“//”为前缀的搜索将会搜索整个文档,而不仅限于当前节点的子节点。使用“.//”将搜索范围限制在此 WebElement 的子节点内。

英文:

Your problem is in the XPath expression. The Java documentation has a clear description:

> When using xpath be aware that webdriver follows standard conventions: a search prefixed with "//" will search the entire document, not just the children of this current node. Use ".//" to limit your search to the children of this WebElement.

huangapple
  • 本文由 发表于 2020年10月28日 01:15:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64559605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定