{"id":3303,"date":"2025-01-16T05:04:35","date_gmt":"2025-01-16T05:04:35","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/leverage-playwright-java-to-access-the-element-using-its-nth-index\/"},"modified":"2025-01-16T05:04:35","modified_gmt":"2025-01-16T05:04:35","slug":"leverage-playwright-java-to-access-the-element-using-its-nth-index","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/leverage-playwright-java-to-access-the-element-using-its-nth-index\/","title":{"rendered":"Leverage Playwright Java to access the element using its Nth index"},"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=\"3303\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will utilize Playwright Java to locate and retrieve the Nth element within a given context.<\/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>Identify Nth element and populate data (positive indexing)<\/li>\n<li>Negative indexing\u00a0<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>Identify Nth element and populate data (positive indexing)<\/strong><\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/rahulshettyacademy.com\/angularpractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/angularpractice\/<\/a>\u00a0 \u00a0<\/p>\n<p>We can see 3 input text fields on this page<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d814343\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3306\" data-css=\"tve-u-1946d8156a9\" data-height=\"314\" data-id=\"3306\" data-init-height=\"684\" data-init-width=\"1168\" data-width=\"536\" decoding=\"async\" height=\"314\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.35.49\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 684;\" title=\"Screenshot 2025-01-16 at 10.35.49\u202fAM\" width=\"536\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Examine any of the text fields and observe that the fields belongs to the &#8216;input&#8217; tagname type<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d81d484\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3307\" data-css=\"tve-u-1946d81e82b\" data-height=\"291\" data-id=\"3307\" data-init-height=\"684\" data-init-width=\"1168\" data-width=\"497\" decoding=\"async\" height=\"291\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.36.28\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 684;\" title=\"Screenshot 2025-01-16 at 10.36.28\u202fAM\" width=\"497\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Utilize the &#8216;input&#8217; locator to populate the text field with a specific string:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d833cc7\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3308\" data-css=\"tve-u-1946d834ddc\" data-height=\"382\" data-id=\"3308\" data-init-height=\"918\" data-init-width=\"1168\" data-width=\"486\" decoding=\"async\" height=\"382\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.38.01\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 918;\" title=\"Screenshot 2025-01-16 at 10.38.01\u202fAM\" width=\"486\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute and take note of the error in the console<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d83de13\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3309\" data-css=\"tve-u-1946d83f15e\" data-height=\"232\" data-id=\"3309\" data-init-height=\"468\" data-init-width=\"1168\" data-width=\"578\" decoding=\"async\" height=\"232\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.38.45\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 468;\" title=\"Screenshot 2025-01-16 at 10.38.45\u202fAM\" width=\"578\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0 The error implies that the playwright has discovered multiple input fields but is uncertain about which input field should it use to enter the text string.<\/p>\n<p>What is the best way to direct the playwright in selecting the first input field?<\/p>\n<p>The reference provided below can be utilized.\u00a0<\/p>\n<p>The indexing of fields starts from 0, with the first field having an index of 0, the second field having an index of 1, and so on<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d844fee\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3310\" data-css=\"tve-u-1946d846664\" data-height=\"225\" data-id=\"3310\" data-init-height=\"468\" data-init-width=\"1168\" data-width=\"562\" decoding=\"async\" height=\"225\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.39.14\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 468;\" title=\"Screenshot 2025-01-16 at 10.39.14\u202fAM\" width=\"562\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The syntax for accessing the &#8216;nth element&#8217; is:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d84c64e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3311\" data-css=\"tve-u-1946d84d90c\" data-height=\"231\" data-id=\"3311\" data-init-height=\"562\" data-init-width=\"1168\" data-width=\"480\" decoding=\"async\" height=\"231\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.39.44\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 562;\" title=\"Screenshot 2025-01-16 at 10.39.44\u202fAM\" width=\"480\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Execute.<\/p>\n<p>See below, in the first text field, the word &#8216;rahulshettyacademy&#8217; is entered. No error this time<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d8599aa\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3312\" data-css=\"tve-u-1946d85aa83\" data-height=\"471\" data-id=\"3312\" data-init-height=\"1048\" data-init-width=\"1168\" data-width=\"525\" decoding=\"async\" height=\"471\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.40.30\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 1048;\" title=\"Screenshot 2025-01-16 at 10.40.30\u202fAM\" width=\"525\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>See below, in the second text field, the word &#8216;rahulshettyacademy.com&#8217; is entered<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d861e81\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3313\" data-css=\"tve-u-1946d862eb5\" data-height=\"356\" data-id=\"3313\" data-init-height=\"768\" data-init-width=\"1168\" data-width=\"542\" decoding=\"async\" height=\"356\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.41.14\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 768;\" title=\"Screenshot 2025-01-16 at 10.41.14\u202fAM\" width=\"542\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We can now utilize the &#8216;Nth&#8217; element approach to populate all three fields<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d86862b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3314\" data-css=\"tve-u-1946d869ea6\" data-height=\"109\" data-id=\"3314\" data-init-height=\"230\" data-init-width=\"1168\" data-width=\"555\" decoding=\"async\" height=\"109\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.41.40\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 230;\" title=\"Screenshot 2025-01-16 at 10.41.40\u202fAM\" width=\"555\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.\u00a0<\/p>\n<p>Notice that all the 3 fields are populated without any errors<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d86f849\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3315\" data-css=\"tve-u-1946d870dbf\" data-height=\"283\" data-id=\"3315\" data-init-height=\"718\" data-init-width=\"1168\" data-width=\"460\" decoding=\"async\" height=\"283\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.42.10\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 718;\" title=\"Screenshot 2025-01-16 at 10.42.10\u202fAM\" width=\"460\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Negative indexing<\/strong><\/p>\n<p>Negative index numbers can also be utilized to identify the fields.\u00a0<\/p>\n<p>The final field will be assigned an index of -1, the second last field will have index of -2 and so forth&#8230;<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d876b75\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3316\" data-css=\"tve-u-1946d8785e6\" data-height=\"236\" data-id=\"3316\" data-init-height=\"572\" data-init-width=\"1168\" data-width=\"481\" decoding=\"async\" height=\"236\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.42.38\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 572;\" title=\"Screenshot 2025-01-16 at 10.42.38\u202fAM\" width=\"481\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let&#8217;s attempt using a negative value of -1<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d880a22\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3317\" data-css=\"tve-u-1946d8822e3\" data-height=\"612\" data-id=\"3317\" data-init-height=\"1514\" data-init-width=\"1168\" data-width=\"472\" decoding=\"async\" height=\"612\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.43.16\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 1514;\" title=\"Screenshot 2025-01-16 at 10.43.16\u202fAM\" width=\"472\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So this is how we can identify \u2018Nth\u2019 element based on its index.<\/p>\n<p><strong>Source code<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> java.nio.file.Paths;<\/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.Download;<\/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> Blog17_IdentifyNthElement {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() <strong>throws<\/strong> InterruptedException {<\/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:\/\/rahulshettyacademy.com\/angularpractice\/&#8221;);<\/p>\n<p>\/\/pg.locator(&#8220;input&#8221;).fill(&#8220;rahulshettyacademy&#8221;);<\/p>\n<p>pg.locator(&#8220;input &gt;&gt; nth=0&#8221;).fill(&#8220;rahulshettyacademy&#8221;);<\/p>\n<p>pg.locator(&#8220;input &gt;&gt; nth=1&#8221;).fill(&#8220;rahulshettyacademy.com&#8221;);<\/p>\n<p>\/\/pg.locator(&#8220;input &gt;&gt; nth=2&#8221;).fill(&#8220;pwd&#8221;);<\/p>\n<p>pg.locator(&#8220;input &gt;&gt; nth=-1&#8221;).fill(&#8220;pwd&#8221;);<\/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 utilize Playwright Java to locate and retrieve the Nth element within a given context.\u00a0 Topics that we will cover: Identify Nth element and populate data (positive indexing) Negative indexing\u00a0 Source code Identify Nth element and populate data (positive indexing) Go to https:\/\/rahulshettyacademy.com\/angularpractice\/\u00a0 \u00a0 We can see 3 input text fields [&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-3303","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\/3303","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=3303"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3303\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}