问题
I need to get the list of all article titles. But for some reason Selenium returns the same instance of the article WebElement 3 times. The web page HTML looks like this:
<div id="content" class="site-content clearfix">
<div class="container clearfix">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<article id="post-403">
<h2 class="entry-title">
<i class="trusted-entry-icon"/>
<a href="https://www.example.com/title-of-article-1/" rel="bookmark">Title of Article 1</a>
</h2>
</article>
<article id="post-404">
<h2 class="entry-title">
<i class="trusted-entry-icon"/>
<a href="https://www.example.com/title-of-article-2/" rel="bookmark">Title of Article 2</a>
</h2>
</article>
<article id="post-405">
<h2 class="entry-title">
<i class="trusted-entry-icon"/>
<a href="https://www.example.com/title-of-article-3/" rel="bookmark">Title of Article 3</a>
</h2>
</article>
</main>
</div>
</div>
Here is the Java code:
List<WebElement> articles = driver.findElements(By.xpath("//article"));
int numberOfArticles = articles.size();
System.out.println("numberOfArticles = " + numberOfArticles);
for (WebElement article : articles){
String articleTitle = article.findElement(By.xpath("//h2/a")).getText();
System.out.println(articleTitle);
}
And here is the output:
numberOfArticles = 3
Title of Article 1
Title of Article 1
Title of Article 1
Can you please help to understand the issue?
回答1:
Your XPath in your for
loop uses //
to find the child elements. This instructs the XPath engine to begin searching for the element from the root of the document, not from the element. Change your find in the loop to the following:
String articleTitle = article.findElement(By.xpath(".//h2/a")).getText();
Note carefully the .
preceding the //
. This tells the XPath engine to only include child nodes of the current (or “context”) node in its results.
回答2:
If I remember correctly, a path starting with / is an absolute path, meaning
"//h2/a"
finds a first h2/a in the whole documment
来源:https://stackoverflow.com/questions/57365986/selenium-findelements-returns-the-same-instance-of-the-first-element-multiple