Usage of texts() method and ‘List’ in Selenide

Tutorial 13​​ ​​ Usage of​​ texts() method​​ and ‘List’​​ in Selenide

What you will Learn in this blog:

  • Usage of​​ texts()​​ method​​ and ‘List

  • Code snippet

Usage of​​ texts()​​ method​​ and ‘List’

Go to​​ https://www.selenium-tutorial.com/​​ and let us try to​​ fetch the footer links on the page.​​ 

The custom xpath​​ //footer//ul/li/a​​ is returning us 7 footer links

Let us first​​ get the number of footer links​​ ​​ 

Execute

Next, let us get the text of each footer link

Execute

Next, let us get the last 3 footer links

Execute

Now let us see the usage of texts() method.​​ 

Selenide has texts() method that comes very handy when we want to fetch the text

As can be seen, the texts() method returns a ‘List’

So we can write

Execute, notice that the text of all the footer links gets printed

Code snippet

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.$$;

import​​ static​​ com.codeborne.selenide.Selenide.open;

import​​ java.net.MalformedURLException;

import​​ java.util.List;

import​​ org.openqa.selenium.By;

import​​ org.testng.annotations.Test;

import​​ com.codeborne.selenide.Configuration;

import​​ com.codeborne.selenide.ElementsCollection;

 

public​​ class​​ ElementsTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

 Configuration.browser​​ =​​ "firefox";

open("https://www.selenium-tutorial.com/");

 ElementsCollection​​ footerl​​ =​​ $$(By.xpath("//footer//ul/li/a"));

 List<String>​​ foo​​ =​​ footerl.texts();

foo.forEach(l​​ -> System.out.println(l));

//System.out.println("The number of footer links-->" + footerl.size());

//footerl.forEach(l -> System.out.println(l.getText()));

//footerl.last(3).forEach(l -> System.out.println(l.getText()));

 Thread.sleep(5000);

 }

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024