In this blog, we will see how to perform a hover action on an element using Playwright Java. We will also see a different approach to click on the text hyperlink.

Topics that we will cover:

  • Alternate approach to click the link
  • Hover an element
  • Source code (click)
  • Source code (hover)

Alternate approach to click the link 

We will now see an alternative approach to clicking on a link.

In one of our previous blog, we have observed how the locator() method can be used to click on a link by its text  

There is also an alternative method to click on a link without using the locator method.

The 'locator' needs to be simply replaced with 'click' as demonstrated below

 Execute the code.

Observe that the link is being clicked

 In the same way

 Execute

 So this is how the click() method can be utilized as a substitute for the locator() method.

Hover an element

Navigate to
https://the-internet.herokuapp.com/hovers 

 Hover over any image, you will see corresponding name

 When we click ‘View profile’, we can see ‘users/2’ in the address

 Let us automate the hover operation over the first user image and click ‘View Profile’. 

Inspect the first image. 

The image can be identified using ‘alt’ attribute  

We can now make use of the ‘hover’ method to hover over the element

So basically we are locating the element using its xpath and than hovering over it (see below to quickly find xpath)

 So let us comment line#27 and uncomment line#26 as shown below

 Execute. 

The first image gets hovered, see below 

Inspect ‘View profile’

 Let us now click the element ‘View profile’ 

 We have used the “first()” method since we are tlling playwright to click the first image.

Execute the code.

‘View profile’ element gets clicked

 

This is how ‘hover’ method can be used in playwright java.

Source code (click)

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 Blog12_clicklink {

@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.click("text = Context Menu");

pg.click("text = dropdown");

pg.pause();

}

}


Source code (hover)

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 Blog12_hover {

@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/hovers");

pg.hover("[alt=\"User Avatar\"]");

//pg.locator("//div[@class='example']//div[1]//img[1]").hover(); //even this works

pg.locator("text=View Profile").first().click();

pg.pause();

}

}


Thanks.


Tags


You may also like

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}