{"id":3152,"date":"2025-01-15T19:11:14","date_gmt":"2025-01-15T19:11:14","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/usage-of-scope-locator-to-handle-dynamic-webtable-in-playwright-java\/"},"modified":"2025-01-15T19:11:14","modified_gmt":"2025-01-15T19:11:14","slug":"usage-of-scope-locator-to-handle-dynamic-webtable-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/usage-of-scope-locator-to-handle-dynamic-webtable-in-playwright-java\/","title":{"rendered":"Usage of \u201cscope\u201d locator to handle dynamic webtable in Playwright Java"},"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=\"3152\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog, we will study about the &#8220;scope&#8221; locator in Playwright Java for effectively handling dynamic webtables.<\/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>\u201c<em>scope<\/em>\u201d locator to select checkbox<\/li>\n<li>Extract data from the entire webtable<span class=\"fr-marker\" data-id=\"0\" data-type=\"false\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" data-id=\"0\" data-type=\"true\" style=\"display: none; line-height: 0;\"><\/span><\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>\u201c<\/strong><strong><em>scope<\/em><\/strong><strong>\u201d locator to select checkbox<\/strong><\/p>\n<p>Go to<br \/>\u00a0<a class=\"\" href=\"https:\/\/datatables.net\/extensions\/select\/examples\/initialisation\/checkbox.html\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/datatables.net\/extensions\/select\/examples\/initialisation\/checkbox.html<\/a> \u00a0<\/p>\n<p>Take note of the dynamic webtable shown below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b6605dc\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3155\" data-css=\"tve-u-1946b661f50\" data-height=\"467\" data-id=\"3155\" data-init-height=\"750\" data-init-width=\"1152\" data-width=\"718\" decoding=\"async\" height=\"467\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.46.03\u202fAM.png\" style=\"aspect-ratio: auto 1152 \/ 750;\" title=\"Screenshot 2025-01-16 at 12.46.03\u202fAM\" width=\"718\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Our objective is to automatically select a checkbox for a specific row.<\/p>\n<p>To fulfill this requirement, we will employ &#8220;<em>scope<\/em>&#8221; approach.<\/p>\n<p>Let us inspect webtable<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b678b38\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3156\" data-css=\"tve-u-1946b679db4\" data-height=\"241\" data-id=\"3156\" data-init-height=\"440\" data-init-width=\"1152\" data-width=\"632\" decoding=\"async\" height=\"241\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.47.52\u202fAM.png\" style=\"aspect-ratio: auto 1152 \/ 440;\" title=\"Screenshot 2025-01-16 at 12.47.52\u202fAM\" width=\"632\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the \u201ccss select query\u201d shown in below diagram <br \/>(which uses the table id).\u00a0<\/p>\n<p>This query fetches 12 rows, the first row being the column headers <br \/>and the actual data starts from 2<sup>nd<\/sup> row onwards \u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b682a57\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3157\" data-css=\"tve-u-1946b683b82\" data-height=\"419\" data-id=\"3157\" data-init-height=\"788\" data-init-width=\"1164\" data-width=\"619\" decoding=\"async\" height=\"419\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.48.29\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 788;\" title=\"Screenshot 2025-01-16 at 12.48.29\u202fAM\" width=\"619\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So the entire table can be located by using the same css query\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b71f927\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3158\" data-css=\"tve-u-1946b720b05\" data-height=\"115\" data-id=\"3158\" data-init-height=\"222\" data-init-width=\"1164\" data-width=\"604\" decoding=\"async\" height=\"115\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.59.16\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 222;\" title=\"Screenshot 2025-01-16 at 12.59.16\u202fAM\" width=\"604\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We know that <em>locator()<\/em> method accepts 2 arguments: selector and options<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b7289f4\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3159\" data-css=\"tve-u-1946b729b58\" data-height=\"216\" data-id=\"3159\" data-init-height=\"400\" data-init-width=\"1164\" data-width=\"629\" decoding=\"async\" height=\"216\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.59.52\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 400;\" title=\"Screenshot 2025-01-16 at 12.59.52\u202fAM\" width=\"629\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0In the first argument, we can use <strong><em>:scope<\/em><\/strong> selector (at least one element must match the scope of the given element)\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b752556\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3162\" data-css=\"tve-u-1946b753c11\" data-height=\"125\" data-id=\"3162\" data-init-height=\"222\" data-init-width=\"1164\" data-width=\"656\" decoding=\"async\" height=\"125\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.00.26\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 222;\" title=\"Screenshot 2025-01-16 at 1.00.26\u202fAM\" width=\"656\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The \u2018<strong><em>setHasText()<\/em><\/strong>\u2019 method can be passed in the options argument<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b76d7bd\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3164\" data-css=\"tve-u-1946b76eb28\" data-height=\"120\" data-id=\"3164\" data-init-height=\"222\" data-init-width=\"1164\" data-width=\"627\" decoding=\"async\" height=\"120\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.03.23\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 222;\" title=\"Screenshot 2025-01-16 at 1.03.23\u202fAM\" width=\"627\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Any person&#8217;s name from the webtable can be passed as an <br \/>argument to the &#8216;<strong><em>setHasText()<\/em><\/strong>&#8216; method<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b7dd93c\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3165\" data-css=\"tve-u-1946b7df142\" data-height=\"113\" data-id=\"3165\" data-init-height=\"222\" data-init-width=\"1164\" data-width=\"590\" decoding=\"async\" height=\"113\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.12.08\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 222;\" title=\"Screenshot 2025-01-16 at 1.12.08\u202fAM\" width=\"590\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We need to select the checkbox that is visible next to this individual.\u00a0<\/p>\n<p>The checkox is uniquely represented by class\u00a0 \u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b7f6046\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3166\" data-css=\"tve-u-1946b7f705d\" data-height=\"252\" data-id=\"3166\" data-init-height=\"484\" data-init-width=\"1164\" data-width=\"605\" decoding=\"async\" height=\"252\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.13.45\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 484;\" title=\"Screenshot 2025-01-16 at 1.13.45\u202fAM\" width=\"605\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The locator method can be invoked again, this time passing the checkbox&#8217;s CSS selector as an argument<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b800348\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3167\" data-css=\"tve-u-1946b801608\" data-height=\"90\" data-id=\"3167\" data-init-height=\"144\" data-init-width=\"1164\" data-width=\"727\" decoding=\"async\" height=\"90\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.14.35\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 144;\" title=\"Screenshot 2025-01-16 at 1.14.35\u202fAM\" width=\"727\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0To select the checkbox, we can proceed by invoking the click() method<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b809727\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3168\" data-css=\"tve-u-1946b80add5\" data-height=\"37\" data-id=\"3168\" data-init-height=\"62\" data-init-width=\"1164\" data-width=\"688\" decoding=\"async\" height=\"37\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.15.12\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 62;\" title=\"Screenshot 2025-01-16 at 1.15.12\u202fAM\" width=\"688\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0In line#29, we have thereforr defined the scope of the webtable we want to act upon<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b81594b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3169\" data-css=\"tve-u-1946b816cb2\" data-height=\"114\" data-id=\"3169\" data-init-height=\"194\" data-init-width=\"1164\" data-width=\"684\" decoding=\"async\" height=\"114\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.16.02\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 194;\" title=\"Screenshot 2025-01-16 at 1.16.02\u202fAM\" width=\"684\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us execute the code.\u00a0<\/p>\n<p>See below. The checkbox gets selected without the need for complex xpath in order to automate this use case<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b81efb2\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3170\" data-css=\"tve-u-1946b81fdd6\" data-height=\"443\" data-id=\"3170\" data-init-height=\"752\" data-init-width=\"1164\" data-width=\"686\" decoding=\"async\" height=\"443\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.16.39\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 752;\" title=\"Screenshot 2025-01-16 at 1.16.39\u202fAM\" width=\"686\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Extract data from the entire webtable<\/strong><\/p>\n<p>To obtain the data from the entire webtable, we can make use of the <strong><em>allInnerTexts()<\/em><\/strong> method, as depicted below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8263d3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3171\" data-css=\"tve-u-1946b827a6e\" data-height=\"106\" data-id=\"3171\" data-init-height=\"172\" data-init-width=\"1164\" data-width=\"719\" decoding=\"async\" height=\"106\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.17.12\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 172;\" title=\"Screenshot 2025-01-16 at 1.17.12\u202fAM\" width=\"719\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<em>forEach()<\/em> method can be used to print all the elements of webtable<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b835cd4\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3172\" data-css=\"tve-u-1946b837101\" data-height=\"390\" data-id=\"3172\" data-init-height=\"668\" data-init-width=\"1164\" data-width=\"679\" decoding=\"async\" height=\"390\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.17.49\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 668;\" title=\"Screenshot 2025-01-16 at 1.17.49\u202fAM\" width=\"679\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>So this is the true potential of the <em>:scope<\/em> selector.<\/p>\n<p><strong>Source code\u00a0<\/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><\/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> Blog10_WebTables {<\/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:\/\/datatables.net\/extensions\/select\/examples\/initialisation\/checkbox.html&#8221;);<\/p>\n<p>Locator webtable = pg.locator(&#8220;table#example tr&#8221;);<\/p>\n<p>\u00a0 \u00a0<\/p>\n<p>webtable.locator(&#8220;:scope&#8221;, <strong>new<\/strong> Locator.LocatorOptions().setHasText(&#8220;Bradley Greer&#8221;)).locator(&#8220;.select-checkbox&#8221;).click();<\/p>\n<p>\u00a0 \u00a0 \/\/System.out.println(&#8220;The number of rows in the webtable&#8211;&gt;&#8221; + webtable.count());<\/p>\n<p>\u00a0 \u00a0 webtable.locator(&#8220;:scope&#8221;).allInnerTexts().forEach(e -&gt; System.<strong><em>out<\/em><\/strong>.println(e));<\/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 the &#8220;scope&#8221; locator in Playwright Java for effectively handling dynamic webtables.\u00a0 Topics that we will cover: \u201cscope\u201d locator to select checkbox Extract data from the entire webtable Source code \u201cscope\u201d locator to select checkbox Go to\u00a0https:\/\/datatables.net\/extensions\/select\/examples\/initialisation\/checkbox.html \u00a0 Take note of the dynamic webtable shown below \u00a0Our objective is [&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-3152","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\/3152","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=3152"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3152\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}