In this blog, we will study about Playwright-Java's locators that provide a powerful way to identify elements on a web page, enhancing automation capabilities.
Topics that we will cover:
- Locating visible text content using ‘text’ locator
- Locating element using the first() function
- Locating element using the last() function
- Source code (locator method)
- Source code (locator with first and last functions)
Locating visible text content using ‘text’ locator
Go to https://the-internet.herokuapp.com/
Let us aim to click on the link provided below by utilizing its visible text

Clicking will lead you to the following page

Let us create a new java class.
Notice below the description of ‘locator’ method “The method returns an element locator that can be used to perform actions on this page / frame. Locator is resolved to the element…..”

Refer the below code snippet.
The element is being located in line#26 by using its text, while in line#27, the link is being clicked

Execute the code.
Notice that Playwright clicks the ‘Broken Images’ link and the below page comes up

This is how we can locate a visible text element.
Locating element using the first() function
Let us now see the usage of first() and last() methods to identify an element.
Navigate to
https://rahulshettyacademy.com/lifetime-access
Numerous courses can be found featuring a button labeled 'ENROLL'

When we click first ‘ENROLL’ button, the below page comes up

To click ‘ENROLL’, comment line#27 and add line#28

Execute and notice the error below.
Playwright is encountering an error message as there are multiple elements on the page labeled 'ENROLL'. We have to indicate the specific element that Playwright should click on

In order to resolve this exception, one possible solution is to employ the inbuilt 'first()' method, see line#28 below

When executed, Playwright performs a click on the first 'ENROLL' link, causing the page to open

Locating element using the last() function
Launch
https://rahulshettyacademy.com/learning-path
On this page there are several courses having ‘Enroll’ buttons

Let us use the last() method to click the last ‘Enroll’ button.
Comment lines#25 and 29.
Add lines#26 and 30

When executed, Playwright performs a click on the last 'Enroll' link, causing the course page to open, see below

So this is how we use ‘text’ locator and first(), last() methods to identify the elements.
Source code (locator method)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog6_TextLocatorFirstLast {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
pg.navigate("https://the-internet.herokuapp.com/");
Locator l = pg.locator("text=Broken Images");
l.click();
pg.pause();
}
}
Source code (locator with first and last functions)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog6_TextLocatorFirstLast {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
//pg.navigate("https://the-internet.herokuapp.com/");
//pg.navigate("https://rahulshettyacademy.com/lifetime-access");
pg.navigate("https://rahulshettyacademy.com/learning-path");
//Locator l = pg.locator("text = Broken Images");
//Locator l = pg.locator("text = ENROLL").first();
Locator l = pg.locator("text = Enroll").last();
l.click();
pg.pause();
}
}
Thanks.