{"id":3573,"date":"2025-01-17T08:47:08","date_gmt":"2025-01-17T08:47:08","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/handle-checkboxes-using-playwright-java\/"},"modified":"2025-01-17T08:47:08","modified_gmt":"2025-01-17T08:47:08","slug":"handle-checkboxes-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/handle-checkboxes-using-playwright-java\/","title":{"rendered":"Handle Checkboxes using 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=\"3573\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will be utilizing Playwright Java to handle checkboxes.<\/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>check()<\/em>\u201d method to select a checkbox<\/li>\n<li>\u201c<em>locator()<\/em> method clubbed with <em>check()<\/em>\u201d method to select a checkbox<\/li>\n<li>Use the &#8220;<em>click()<\/em>&#8221; function to pick a checkbox<\/li>\n<li>\u201c<em>uncheck()<\/em>\u201d method to uncheck a checkbox<\/li>\n<li>\u201c<em>locator()<\/em> method clubbed with <em>click()<\/em>\u201d method to select a checkbox<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>\u201c<\/strong><strong><em>check()<\/em><\/strong><strong>\u201d method to select a checkbox\u00a0<\/strong><\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a>\u00a0<\/p>\n<p>Observe below that none of the checkboxes are selected by default<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473745175\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3576\" data-css=\"tve-u-1947374606c\" data-height=\"189\" data-id=\"3576\" data-init-height=\"452\" data-init-width=\"1170\" data-width=\"489\" decoding=\"async\" height=\"189\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.17.58\u202fPM.png\" style=\"aspect-ratio: auto 1170 \/ 452;\" title=\"Screenshot 2025-01-17 at 2.17.58\u202fPM\" width=\"489\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us see how to select a checkbox, for example \u2018Option1\u2019.<\/p>\n<p>Let us inspect \u2018Option1\u2019<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473752cc0\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3577\" data-css=\"tve-u-19473754072\" data-height=\"221\" data-id=\"3577\" data-init-height=\"514\" data-init-width=\"1170\" data-width=\"502\" decoding=\"async\" height=\"221\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.18.37\u202fPM.png\" style=\"aspect-ratio: auto 1170 \/ 514;\" title=\"Screenshot 2025-01-17 at 2.18.37\u202fPM\" width=\"502\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us use \u201ccheck()\u201d method to select the checkbox and pass the \u2018id\u2019 attribute (line#29)<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473774c45\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3580\" data-css=\"tve-u-19473775f46\" data-height=\"783\" data-id=\"3580\" data-init-height=\"1466\" data-init-width=\"1170\" data-width=\"625\" decoding=\"async\" height=\"783\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.19.43\u202fPM-2.png\" style=\"aspect-ratio: auto 1170 \/ 1466;\" title=\"Screenshot 2025-01-17 at 2.19.43\u202fPM\" width=\"625\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>\u201c<\/strong><strong><em>locator()<\/em><\/strong><strong> method clubbed with <\/strong><strong><em>check()<\/em><\/strong><strong>\u201d method to select a checkbox<\/strong><\/p>\n<p>We can also use the \u201c<em>locator()<\/em>\u201d method clubbed with \u201c<em>check()<\/em>\u201d method to select a checkbox (line#32)\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947377fe76\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3581\" data-css=\"tve-u-194737811fd\" data-height=\"566\" data-id=\"3581\" data-init-height=\"1330\" data-init-width=\"1170\" data-width=\"498\" decoding=\"async\" height=\"566\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.22.01\u202fPM.png\" style=\"aspect-ratio: auto 1170 \/ 1330;\" title=\"Screenshot 2025-01-17 at 2.22.01\u202fPM\" width=\"498\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Use the &#8220;<\/strong><strong><em>click()<\/em><\/strong><strong>&#8221; function to pick a checkbox<\/strong><\/p>\n<p>We can also use the \u201c<em>click()<\/em>\u201d method to select a checkbox (see line#35)<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947378a251\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3582\" data-css=\"tve-u-1947378b8ac\" data-height=\"643\" data-id=\"3582\" data-init-height=\"1414\" data-init-width=\"1170\" data-width=\"532\" decoding=\"async\" height=\"643\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.22.41\u202fPM.png\" style=\"aspect-ratio: auto 1170 \/ 1414;\" title=\"Screenshot 2025-01-17 at 2.22.41\u202fPM\" width=\"532\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>\u201c<\/strong><strong><em>uncheck()<\/em><\/strong><strong>\u201d method to uncheck a checkbox<\/strong><\/p>\n<p>We can uncheck a checkbox using \u201c<em>uncheck()<\/em>\u201d method<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947379637a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3583\" data-css=\"tve-u-19473797206\" data-height=\"467\" data-id=\"3583\" data-init-height=\"1124\" data-init-width=\"1170\" data-width=\"486\" decoding=\"async\" height=\"467\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.23.26\u202fPM.png\" style=\"aspect-ratio: auto 1170 \/ 1124;\" title=\"Screenshot 2025-01-17 at 2.23.26\u202fPM\" width=\"486\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>\u201c<\/strong><strong><em>locator()<\/em><\/strong><strong> method clubbed with <\/strong><strong><em>click()<\/em><\/strong><strong>\u201d method to select a checkbox<\/strong><\/p>\n<p>We can also use the \u201c<em>locator()<\/em>\u201d method clubbed with \u201c<em>click()<\/em>\u201d method to select a checkbox<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947379e5e9\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3584\" data-css=\"tve-u-194737a075e\" data-height=\"907\" data-id=\"3584\" data-init-height=\"1386\" data-init-width=\"1170\" data-width=\"766\" decoding=\"async\" height=\"907\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.24.05\u202fPM.png\" style=\"aspect-ratio: auto 1170 \/ 1386;\" title=\"Screenshot 2025-01-17 at 2.24.05\u202fPM\" width=\"766\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<\/p>\n<p>So this is how we can use the various methods to select a checkbox.<\/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> org.testng.annotations.AfterMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.BeforeMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.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.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> Blog33_Checkboxes_PWJava {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>page = browser.newPage();<\/p>\n<p>}<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> test_1() {<\/p>\n<p>page.navigate(&#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;);<\/p>\n<p>\/\/Method 1<\/p>\n<p>page.check(&#8220;[id=&#8217;checkBoxOption1&#8242;]&#8221;);<\/p>\n<p>page.uncheck(&#8220;[id=&#8217;checkBoxOption1&#8242;]&#8221;);<\/p>\n<p>\/\/Method 2<\/p>\n<p>page.locator(&#8220;[id=&#8217;checkBoxOption2&#8242;]&#8221;).check();<\/p>\n<p>\/\/Method 3<\/p>\n<p>page.click(&#8220;[id=&#8217;checkBoxOption3&#8242;]&#8221;);<\/p>\n<p>\/\/Method 4<\/p>\n<p>page.locator(&#8220;[id=&#8217;checkBoxOption1&#8242;]&#8221;).click();<\/p>\n<p>}<\/p>\n<p>@AfterMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> tearDown() {<\/p>\n<p>page.pause();<\/p>\n<p>browser.close();<\/p>\n<p>page.close();<\/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 be utilizing Playwright Java to handle checkboxes. Topics that we will cover: \u201ccheck()\u201d method to select a checkbox \u201clocator() method clubbed with check()\u201d method to select a checkbox Use the &#8220;click()&#8221; function to pick a checkbox \u201cuncheck()\u201d method to uncheck a checkbox \u201clocator() method clubbed with click()\u201d method to select [&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-3573","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\/3573","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=3573"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3573\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}