{"id":3082,"date":"2025-01-14T15:31:43","date_gmt":"2025-01-14T15:31:43","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3082"},"modified":"2025-01-17T10:30:24","modified_gmt":"2025-01-17T10:30:24","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><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>&nbsp;<\/em><\/p>\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\/\" target=\"_blank\" rel=\"noopener\">https:\/\/the-internet.herokuapp.com\/<\/a>&nbsp;<\/p>\n<p>Let us aim to click on the link provided below by utilizing its visible text<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3085\" width=\"475\" data-init-width=\"852\" height=\"287\" data-init-height=\"514\" title=\"Screenshot 2025-01-14 at 9.03.21\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.03.21\u202fPM.png\" data-width=\"475\" data-height=\"287\" style=\"aspect-ratio: auto 852 \/ 514;\"><\/span><\/p>\n<p>Clicking will lead you to the following page<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3086\" width=\"501\" data-init-width=\"1128\" height=\"230\" data-init-height=\"518\" title=\"Screenshot 2025-01-14 at 9.04.03\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.04.03\u202fPM.png\" data-width=\"501\" data-height=\"230\" style=\"aspect-ratio: auto 1128 \/ 518;\"><\/span><\/p>\n<p>Let us create a new java class.&nbsp;<\/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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3087\" width=\"616\" data-init-width=\"1142\" height=\"279\" data-init-height=\"518\" title=\"Screenshot 2025-01-14 at 9.04.45\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.04.45\u202fPM.png\" data-width=\"616\" data-height=\"279\" style=\"aspect-ratio: auto 1142 \/ 518;\"><\/span><\/p>\n<p>&nbsp;Refer 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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3088\" width=\"595\" data-init-width=\"1142\" height=\"331\" data-init-height=\"636\" title=\"Screenshot 2025-01-14 at 9.05.26\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.05.26\u202fPM.png\" data-width=\"595\" data-height=\"331\" style=\"aspect-ratio: auto 1142 \/ 636;\"><\/span><\/p>\n<p>&nbsp;Execute the code.&nbsp;<\/p>\n<p>Notice that Playwright clicks the \u2018Broken Images\u2019 link and the below page comes up<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3089\" width=\"646\" data-init-width=\"1142\" height=\"346\" data-init-height=\"612\" title=\"Screenshot 2025-01-14 at 9.06.08\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.06.08\u202fPM.png\" data-width=\"646\" data-height=\"346\" style=\"aspect-ratio: auto 1142 \/ 612;\"><\/span><\/p>\n<p>&nbsp;This 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&nbsp;<\/p>\n<p>Numerous courses can be found featuring a button labeled &#8216;ENROLL&#8217;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3090\" width=\"602\" data-init-width=\"1172\" height=\"412\" data-init-height=\"802\" title=\"Screenshot 2025-01-14 at 9.06.49\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.06.49\u202fPM.png\" data-width=\"602\" data-height=\"412\" style=\"aspect-ratio: auto 1172 \/ 802;\"><\/span><\/p>\n<p>&nbsp;When we click first \u2018ENROLL\u2019 button, the below page comes up<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3091\" width=\"603\" data-init-width=\"1172\" height=\"458\" data-init-height=\"890\" title=\"Screenshot 2025-01-14 at 9.07.32\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.07.32\u202fPM.png\" data-width=\"603\" data-height=\"458\" style=\"aspect-ratio: auto 1172 \/ 890;\"><\/span><\/p>\n<p>&nbsp;To click \u2018ENROLL\u2019, comment line#27 and add line#28<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3092\" width=\"648\" data-init-width=\"1172\" height=\"389\" data-init-height=\"704\" title=\"Screenshot 2025-01-14 at 9.08.11\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.08.11\u202fPM.png\" data-width=\"648\" data-height=\"389\" style=\"aspect-ratio: auto 1172 \/ 704;\"><\/span><\/p>\n<p>&nbsp;Execute and notice the error below.&nbsp;<\/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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3093\" width=\"640\" data-init-width=\"1172\" height=\"298\" data-init-height=\"546\" title=\"Screenshot 2025-01-14 at 9.08.48\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.08.48\u202fPM.png\" data-width=\"640\" data-height=\"298\" style=\"aspect-ratio: auto 1172 \/ 546;\"><\/span><\/p>\n<p>&nbsp;In order to resolve this exception, one possible solution is to employ the inbuilt &#8216;first()&#8217; method, see line#28 below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3094\" width=\"605\" data-init-width=\"1172\" height=\"363\" data-init-height=\"704\" title=\"Screenshot 2025-01-14 at 9.09.23\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.09.23\u202fPM.png\" data-width=\"605\" data-height=\"363\" style=\"aspect-ratio: auto 1172 \/ 704;\"><\/span><\/p>\n<p>&nbsp;When executed, Playwright performs a click on the first &#8216;ENROLL&#8217; link, causing the page to open<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3095\" width=\"608\" data-init-width=\"1172\" height=\"353\" data-init-height=\"680\" title=\"Screenshot 2025-01-14 at 9.10.49\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.10.49\u202fPM.png\" data-width=\"608\" data-height=\"353\" style=\"aspect-ratio: auto 1172 \/ 680;\"><\/span><\/p>\n<p>&nbsp;<\/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>&nbsp;<\/p>\n<p>On this page there are several courses having \u2018Enroll\u2019 buttons&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3096\" width=\"558\" data-init-width=\"1172\" height=\"661\" data-init-height=\"1388\" title=\"Screenshot 2025-01-14 at 9.11.29\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.11.29\u202fPM.png\" data-width=\"558\" data-height=\"661\" style=\"aspect-ratio: auto 1172 \/ 1388;\"><\/span><\/p>\n<p>&nbsp;Let us use the last() method to click the last \u2018Enroll\u2019 button.&nbsp;<\/p>\n<p>Comment lines#25 and 29.<\/p>\n<p>Add lines#26 and 30&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3097\" width=\"566\" data-init-width=\"1172\" height=\"320\" data-init-height=\"662\" title=\"Screenshot 2025-01-14 at 9.12.34\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.12.34\u202fPM.png\" data-width=\"566\" data-height=\"320\" style=\"aspect-ratio: auto 1172 \/ 662;\"><\/span><\/p>\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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3098\" width=\"621\" data-init-width=\"1172\" height=\"396\" data-init-height=\"748\" title=\"Screenshot 2025-01-14 at 9.13.10\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.13.10\u202fPM.png\" data-width=\"621\" data-height=\"396\" style=\"aspect-ratio: auto 1172 \/ 748;\"><\/span><\/p>\n<p>&nbsp;So 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>\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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog6_TextLocatorFirstLast {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><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>\n<p><strong>Source code (locator with first and last functions)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog6_TextLocatorFirstLast {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><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>\n<p>Thanks.<\/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.&nbsp; 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":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","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":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3082\/revisions"}],"predecessor-version":[{"id":3697,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3082\/revisions\/3697"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"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}]}}