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?