Locators in Playwright-Java

Tutorial 6​​ ​​ Locators​​ in​​ Playwright-Java​​ 

What you will Learn​​ in this​​ blog:

  • Locating​​ element using​​ ‘text’ locator

  • Use of first(), last() to locate the elements

  • Code snippets

Locating element using​​ ‘text’ locator

The use case is: we would like to click the link shown below using it’s text

After clicking, the below page should come up

Before we go ahead and locate an element, let us (for time being) remove the junit dependencies from pom.xml.

Make sure you only have playwright dependency

Create new java class

The lines#20-21 are self-explanatory.​​ 

In line#20, we are locating the element using its text and in line#21, we are simply clicking the link

Save and execute as java application.​​ 

Notice that the test is successful and the Playwright clicks the link

Let us see another example, launch​​ https://www.way2automation.com/lifetime-membership-club/​​ 

You would see many courses with the button​​ having the​​ text ‘Get Started’

Let us try to click ‘Get Started’, change the code as shown

Save and execute, notice the error.​​ 

This is because Playwright is complaining that there are many elements on the page having the text ‘Get Started’, which one do you want me to click?

To resolve this, we can use the inbuilt ‘first()’ method

Save and execute, notice that PW clicks the link and the course page opens

We can count the number of elements having the text ‘Get Started’​​ 

Save/execute, the count is 16

Let us now click the last webelement having the text ‘Get Started’​​ (using last() method)

Save/execute

We will continue with locators in our next blog.

Code Snippets

package​​ com.w2a.pwjava;

 

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Locator;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ ClickElementUsingText {

 

public​​ static​​ void​​ main(String[]​​ args) {

 Playwright​​ pw​​ = Playwright.create();

 Browser​​ browser​​ =​​ pw.chromium().launch(new​​ BrowserType.LaunchOptions().setHeadless(false));

 

 BrowserContext​​ browserContext​​ =​​ browser.newContext();

 Page​​ page​​ =​​ browserContext.newPage();

//page.navigate("https://the-internet.herokuapp.com/");

page.navigate("https://www.way2automation.com/lifetime-membership-club/");

//Locator​​ loc​​ = page.locator("text = A/B Testing");

//Locator​​ loc​​ = page.locator("text = Get Started").first();

//loc.click();

 Locator​​ loc​​ =​​ page.locator("text = Get Started");

int​​ getStartedbtns​​ =​​ loc.count();

 System.out.println("The count of Get Started:-->"​​ +​​ getStartedbtns);

loc.last().click();

 

 }

 

}

 

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 1st Nov 2024