{"id":3082,"date":"2025-01-14T15:31:43","date_gmt":"2025-01-14T15:31:43","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/14\/playwright-javas-text-locator-and-usage-of-first-and-last-method\/"},"modified":"2025-01-14T15:31:43","modified_gmt":"2025-01-14T15:31:43","slug":"playwright-javas-text-locator-and-usage-of-first-and-last-method","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/14\/playwright-javas-text-locator-and-usage-of-first-and-last-method\/","title":{"rendered":"Playwright-Java\u2019s \u2018text\u2019 locator and usage of first() and last() method"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3082\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog, we will study about Playwright-Java&#8217;s locators that provide a powerful way to identify elements on a web page, enhancing automation capabilities.<\/em><em>\u00a0<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Locating visible text content using \u2018text\u2019 locator<\/li>\n<li>Locating element using the first() function<\/li>\n<li>Locating element using the last() function<\/li>\n<li>Source code (locator method)<\/li>\n<li>Source code (locator with first and last functions)<\/li>\n<\/ul>\n<p><strong>Locating visible text content using \u2018text\u2019 locator<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/the-internet.herokuapp.com\/\" rel=\"noopener\" target=\"_blank\">https:\/\/the-internet.herokuapp.com\/<\/a>\u00a0<\/p>\n<p>Let us aim to click on the link provided below by utilizing its visible text<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465731240\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3085\" data-css=\"tve-u-19465732981\" data-height=\"287\" data-id=\"3085\" data-init-height=\"514\" data-init-width=\"852\" data-width=\"475\" decoding=\"async\" height=\"287\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.03.21\u202fPM.png\" style=\"aspect-ratio: auto 852 \/ 514;\" title=\"Screenshot 2025-01-14 at 9.03.21\u202fPM\" width=\"475\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Clicking will lead you to the following page<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946573a5b3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3086\" data-css=\"tve-u-1946573ba91\" data-height=\"230\" data-id=\"3086\" data-init-height=\"518\" data-init-width=\"1128\" data-width=\"501\" decoding=\"async\" height=\"230\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.04.03\u202fPM.png\" style=\"aspect-ratio: auto 1128 \/ 518;\" title=\"Screenshot 2025-01-14 at 9.04.03\u202fPM\" width=\"501\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Let us create a new java class.\u00a0<\/p>\n<p>Notice below the description of \u2018locator\u2019 method \u201c<em>The method returns an element locator that can be used to perform actions on this page \/ frame. Locator is resolved to the element\u2026..<\/em>\u201d<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465745b83\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3087\" data-css=\"tve-u-19465746f2b\" data-height=\"279\" data-id=\"3087\" data-init-height=\"518\" data-init-width=\"1142\" data-width=\"616\" decoding=\"async\" height=\"279\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.04.45\u202fPM.png\" style=\"aspect-ratio: auto 1142 \/ 518;\" title=\"Screenshot 2025-01-14 at 9.04.45\u202fPM\" width=\"616\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Refer the below code snippet.<\/p>\n<p>The element is being located in line#26 by using its text, while in line#27, the link is being clicked<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946574ffbd\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3088\" data-css=\"tve-u-1946575108e\" data-height=\"331\" data-id=\"3088\" data-init-height=\"636\" data-init-width=\"1142\" data-width=\"595\" decoding=\"async\" height=\"331\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.05.26\u202fPM.png\" style=\"aspect-ratio: auto 1142 \/ 636;\" title=\"Screenshot 2025-01-14 at 9.05.26\u202fPM\" width=\"595\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/p>\n<p>Notice that Playwright clicks the \u2018Broken Images\u2019 link and the below page comes up<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465759343\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3089\" data-css=\"tve-u-1946575a8c1\" data-height=\"346\" data-id=\"3089\" data-init-height=\"612\" data-init-width=\"1142\" data-width=\"646\" decoding=\"async\" height=\"346\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.06.08\u202fPM.png\" style=\"aspect-ratio: auto 1142 \/ 612;\" title=\"Screenshot 2025-01-14 at 9.06.08\u202fPM\" width=\"646\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This is how we can locate a visible text element.<\/p>\n<p><strong>Locating element using the first() function<\/strong><\/p>\n<p>Let us now see the usage of <em>first()<\/em> and <em>last()<\/em> methods to identify an element.<\/p>\n<p>Navigate to<\/p>\n<p>https:\/\/rahulshettyacademy.com\/lifetime-access\u00a0<\/p>\n<p>Numerous courses can be found featuring a button labeled &#8216;ENROLL&#8217;<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465762ec3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3090\" data-css=\"tve-u-19465764195\" data-height=\"412\" data-id=\"3090\" data-init-height=\"802\" data-init-width=\"1172\" data-width=\"602\" decoding=\"async\" height=\"412\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.06.49\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 802;\" title=\"Screenshot 2025-01-14 at 9.06.49\u202fPM\" width=\"602\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0When we click first \u2018ENROLL\u2019 button, the below page comes up<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946576d304\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3091\" data-css=\"tve-u-1946576ebcd\" data-height=\"458\" data-id=\"3091\" data-init-height=\"890\" data-init-width=\"1172\" data-width=\"603\" decoding=\"async\" height=\"458\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.07.32\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 890;\" title=\"Screenshot 2025-01-14 at 9.07.32\u202fPM\" width=\"603\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0To click \u2018ENROLL\u2019, comment line#27 and add line#28<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194657773ce\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3092\" data-css=\"tve-u-19465778b7f\" data-height=\"389\" data-id=\"3092\" data-init-height=\"704\" data-init-width=\"1172\" data-width=\"648\" decoding=\"async\" height=\"389\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.08.11\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 704;\" title=\"Screenshot 2025-01-14 at 9.08.11\u202fPM\" width=\"648\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute and notice the error below.\u00a0<\/p>\n<p>Playwright is encountering an error message as there are multiple elements on the page labeled &#8216;ENROLL&#8217;. We have to indicate the specific element that Playwright should click on<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946578008b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3093\" data-css=\"tve-u-194657817c3\" data-height=\"298\" data-id=\"3093\" data-init-height=\"546\" data-init-width=\"1172\" data-width=\"640\" decoding=\"async\" height=\"298\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.08.48\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 546;\" title=\"Screenshot 2025-01-14 at 9.08.48\u202fPM\" width=\"640\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0In order to resolve this exception, one possible solution is to employ the inbuilt &#8216;first()&#8217; method, see line#28 below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465788539\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3094\" data-css=\"tve-u-19465789c47\" data-height=\"363\" data-id=\"3094\" data-init-height=\"704\" data-init-width=\"1172\" data-width=\"605\" decoding=\"async\" height=\"363\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.09.23\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 704;\" title=\"Screenshot 2025-01-14 at 9.09.23\u202fPM\" width=\"605\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0When executed, Playwright performs a click on the first &#8216;ENROLL&#8217; link, causing the page to open<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946579d780\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3095\" data-css=\"tve-u-1946579ec38\" data-height=\"353\" data-id=\"3095\" data-init-height=\"680\" data-init-width=\"1172\" data-width=\"608\" decoding=\"async\" height=\"353\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.10.49\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 680;\" title=\"Screenshot 2025-01-14 at 9.10.49\u202fPM\" width=\"608\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<\/p>\n<p><strong>Locating element using the last() function<\/strong><\/p>\n<p>Launch<\/p>\n<p><a href=\"https:\/\/rahulshettyacademy.com\/learning-path\">https:\/\/rahulshettyacademy.com\/learning-path<\/a>\u00a0<\/p>\n<p>On this page there are several courses having \u2018Enroll\u2019 buttons\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194657a8eb8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3096\" data-css=\"tve-u-194657aa816\" data-height=\"661\" data-id=\"3096\" data-init-height=\"1388\" data-init-width=\"1172\" data-width=\"558\" decoding=\"async\" height=\"661\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.11.29\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 1388;\" title=\"Screenshot 2025-01-14 at 9.11.29\u202fPM\" width=\"558\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us use the last() method to click the last \u2018Enroll\u2019 button.\u00a0<\/p>\n<p>Comment lines#25 and 29.<\/p>\n<p>Add lines#26 and 30\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194657b6d01\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3097\" data-css=\"tve-u-194657b7fbc\" data-height=\"320\" data-id=\"3097\" data-init-height=\"662\" data-init-width=\"1172\" data-width=\"566\" decoding=\"async\" height=\"320\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.12.34\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 662;\" title=\"Screenshot 2025-01-14 at 9.12.34\u202fPM\" width=\"566\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>When executed, Playwright performs a click on the last &#8216;Enroll&#8217; link, causing the course page to open, see below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194657c02d8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3098\" data-css=\"tve-u-194657c2766\" data-height=\"396\" data-id=\"3098\" data-init-height=\"748\" data-init-width=\"1172\" data-width=\"621\" decoding=\"async\" height=\"396\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.13.10\u202fPM.png\" style=\"aspect-ratio: auto 1172 \/ 748;\" title=\"Screenshot 2025-01-14 at 9.13.10\u202fPM\" width=\"621\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So this is how we use \u2018text\u2019 locator and first(), last() methods to identify the elements.<\/p>\n<p><strong>Source code (locator method)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Locator;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog6_TextLocatorFirstLast {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>pg.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/&#8221;);<\/p>\n<p>Locator l = pg.locator(&#8220;text=Broken Images&#8221;);<\/p>\n<p>l.click();<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (locator with first and last functions)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Locator;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog6_TextLocatorFirstLast {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>\/\/pg.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/&#8221;);<\/p>\n<p>\/\/pg.navigate(&#8220;https:\/\/rahulshettyacademy.com\/lifetime-access&#8221;);<\/p>\n<p>pg.navigate(&#8220;https:\/\/rahulshettyacademy.com\/learning-path&#8221;);<\/p>\n<p>\/\/Locator l = pg.locator(&#8220;text = Broken Images&#8221;);<\/p>\n<p>\/\/Locator l = pg.locator(&#8220;text = ENROLL&#8221;).first();<\/p>\n<p>Locator l = pg.locator(&#8220;text = Enroll&#8221;).last();<\/p>\n<p>l.click();<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>Thanks.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will study about Playwright-Java&#8217;s locators that provide a powerful way to identify elements on a web page, enhancing automation capabilities.\u00a0 Topics that we will cover: Locating visible text content using \u2018text\u2019 locator Locating element using the first() function Locating element using the last() function Source code (locator method) Source code (locator [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3082","post","type-post","status-publish","format-standard","hentry","category-java","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3082"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3082\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}