Redeem values with equal class names using Selenium

Asked

Viewed 1,589 times

1

I have a situation where I need to rescue values from a array of article's, however I’m not trying very successfully. My HTML is this way:

<section class="list">
    <article class="card">
        Article by Bigown
    </article>
    <article class="card">
        Article by Bacco
    </article>
    <article class="card">
        Article by Wallace
    </article>
    <article class="card">
        Article by Guilherme
    </article>
    <article class="card">
        Article by Marcelo
    </article>
</section>

Using JAVA, I can only capture the value of the first <article> passing the class, but I can’t get hold of the next ones because the classes have equal names. See:

WebElement txtArticle;
txtArticle = driver.findElement(By.className("card"));
System.out.print(title.getText()); 

Return referring to the above code:

Article by Bigown

As I wished to return:

Article by Bigown

Article by Bacco

Article by Wallace

Article by Guilherme

Article by Marcelo

How can I redeem the values of each article even containing equal classes using Selenium?

1 answer

3


Actually the problem is in find, in Selenium when you use driver.findElement by default he catches the first, in this case you can catch driver.findElements for example:

        List<WebElement> txtArticle = driver.findElements(By.className("card"));
        for (WebElement title : txtArticle) {
            System.out.print(title.getText());
        }
  • Man, take away a doubt. How do I pick up values only within a certain div?

  • You can pick up by Xpath, for example if I have <div id="my_div>&#xA; <a>a1</a>&#xA; <a>a2</a>&#xA; <a>a2</a>&#xA;</div> To catch all the <a> from inside this div would be something like: List<WebElement> anchors = driver.findElements(By.xpath("//div[@id='my_div']/a")); In this Xpath it will fetch the div that has the my_div id, after finding it will filter all the Anchors from within this div. Ai with respect to Xpath, you can filter by other attributes tbm, in case if my div has a specific class my Xpath can be //div[@class='my_class'] for example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.