Work with dropdowns using Playwright-Java

Tutorial 7​​ ​​ Work with dropdowns​​ using​​ Playwright-Java

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

  • Fetch and count dropdown options (method 1)

  • Fetch and count dropdown options (method 2)

  • Fetch and​​ count dropdown options (method 3)

  • Use ‘Record’ feature to help select​​ value​​ from dropdown​​ 

  • Code snippets

Fetch and count dropdown options (method 1)

Launch

https://www.way2automation.com/way2auto_jquery/dropdown.php#load_box​​ 

The use case that we’d automate now is: count the number of countries in the ‘Select Country’ dropdown and print all of them

Inspect the dropdown, it is ‘select’ tag

We can expand the ‘select’ tag to see all the ‘otions’. On executing the ‘select option’ locator query, we see total number of countries#239​​ 

We can now use the same locator ‘select option’ in line#21 and print the total count

Save and execute, the​​ same​​ count​​ 239 is printed in console

When we click the dropdown, we see lot of countries

We can fetch the text of all these countires using a ‘for’ loop (lines#25-28).​​ 

We can use the inbuilt ‘nth()’ method that in-turn calls the textContent() method ​​ 

Save and execute


Fetch and​​ count dropdown options (method 2)

We can also use slightly simpler method to print the text of all the countries using ‘List’.

We can directly call the ‘allTextContents()’ method

Comment method#1.

Save and execute

Fetch and​​ count dropdown options (method 3)

We can also use​​ a​​ further​​ much simpler ‘forEach’ loop, see Method#3

Comment method#2.

Save and execute

Use ‘Record’ feature to help select value from dropdown​​ 

We will now see how to select a value from dropdown

Let us​​ use the ‘select’ locator to​​ select​​ ‘Albania’ from the dropdown (see line#29)

Save and execute the script.

We get an error, see below.​​ 

If we carefully observe the error, it is hinting us that the webpage is a frame and hence we can’t simply select an element from the frame

If we right click this page, it confirms that the page is​​ indeed​​ a frame

Let us put a page.pause() debug​​ statement at the end of the script​​ (to open the inspector), so that we can record the action to select a value from dropdown

Save and execute.

Click ‘Record’ to start recording the events.

Click the dropdown and select ‘Albania’. Notice that the corresponding line that’s recorded in the inspector and it indeed is a frameLocator

Copy this line and paste it in the script

Stop recording by clicking ‘Record’ button​​ 

Close inspector

Comment debug line

Execute script.​​ 

This time, ‘Albania’ gets selected, see below

So this is how​​ we can work with​​ dropdown.

Code snippet

package​​ com.w2a.pwjava;

 

//import java.awt.List;

import​​ java.util.List;

 

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​​ CountFetchDropdownOptions {

 

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://www.way2automation.com/way2auto_jquery/dropdown.php#load_box");

 Locator​​ loc​​ =​​ page.locator("select option");

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

 System.out.println("The count of countries:-->"​​ +​​ country_cnt);

//select​​ dropdown​​ value

page.frameLocator("#example-1-tab-1 iframe").locator("select").selectOption("Albania");

 

/*

 //Method 1

 for(int​​ i=0; i<loc.count(); i++) {

 String country_text = loc.nth(i).textContent();

 System.out.println(country_text);

 }

 //Method 2

 List<String> country_text = loc.allTextContents();

 for(String​​ str: country_text) {

 System.out.println(str);

 }

 //Method 3

 List<String> country_text = loc.allTextContents();

 country_text.forEach(str​​ -> System.out.println(str));

 */

//page.pause();

 

 }

}

 

 

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