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!