{"id":3559,"date":"2025-01-17T08:39:24","date_gmt":"2025-01-17T08:39:24","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/handle-radio-buttons-using-playwright-java\/"},"modified":"2025-01-17T08:39:24","modified_gmt":"2025-01-17T08:39:24","slug":"handle-radio-buttons-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/handle-radio-buttons-using-playwright-java\/","title":{"rendered":"Handle Radio buttons 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=\"3559\" 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 radio buttons.<\/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 radio button<\/li>\n<li>\u201c<em>locator()<\/em> method clubbed with <em>check()<\/em>\u201d method to select a radio button<\/li>\n<li>Use the &#8220;<em>click()<\/em>&#8221; function to pick a radio button<\/li>\n<li>\u201c<em>locator()<\/em> method clubbed with <em>click()<\/em>\u201d method to select a radio button<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>\u201c<\/strong><strong><em>check()<\/em><\/strong><strong>\u201d method to select a radio button\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 radio buttons are selected by default<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194736d1c9d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3562\" data-css=\"tve-u-194736d2a5d\" data-height=\"259\" data-id=\"3562\" data-init-height=\"568\" data-init-width=\"1174\" data-width=\"536\" decoding=\"async\" height=\"259\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.10.13\u202fPM.png\" style=\"aspect-ratio: auto 1174 \/ 568;\" title=\"Screenshot 2025-01-17 at 2.10.13\u202fPM\" width=\"536\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us see how to select a radio button, for example \u2018Radio1\u2019.<\/p>\n<p>Let us inspect \u2018Radio1\u2019<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194736da73b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3563\" data-css=\"tve-u-194736db96d\" data-height=\"337\" data-id=\"3563\" data-init-height=\"784\" data-init-width=\"1174\" data-width=\"505\" decoding=\"async\" height=\"337\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.10.56\u202fPM.png\" style=\"aspect-ratio: auto 1174 \/ 784;\" title=\"Screenshot 2025-01-17 at 2.10.56\u202fPM\" width=\"505\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us use \u201ccheck()\u201d method to select the radio button and pass the \u2018value\u2019 attribute (line#29)<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194736e2e10\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3564\" data-css=\"tve-u-194736e3da5\" data-height=\"336\" data-id=\"3564\" data-init-height=\"706\" data-init-width=\"1174\" data-width=\"558\" decoding=\"async\" height=\"336\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.11.32\u202fPM.png\" style=\"aspect-ratio: auto 1174 \/ 706;\" title=\"Screenshot 2025-01-17 at 2.11.32\u202fPM\" width=\"558\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Run the script.\u00a0<\/p>\n<p>Notice below that \u201cRadio1\u201d gets selected<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194736ea022\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3565\" data-css=\"tve-u-194736eb092\" data-height=\"331\" data-id=\"3565\" data-init-height=\"816\" data-init-width=\"1174\" data-width=\"476\" decoding=\"async\" height=\"331\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.12.00\u202fPM.png\" style=\"aspect-ratio: auto 1174 \/ 816;\" title=\"Screenshot 2025-01-17 at 2.12.00\u202fPM\" width=\"476\"\/><\/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>check()<\/em><\/strong><strong>\u201d method to select a radio button<\/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 radio button\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194736f38cc\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3566\" data-css=\"tve-u-194736f4e25\" data-height=\"552\" data-id=\"3566\" data-init-height=\"1412\" data-init-width=\"1174\" data-width=\"459\" decoding=\"async\" height=\"552\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.12.29\u202fPM.png\" style=\"aspect-ratio: auto 1174 \/ 1412;\" title=\"Screenshot 2025-01-17 at 2.12.29\u202fPM\" width=\"459\"\/><\/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 radio button<\/strong><\/p>\n<p>We can also use the \u201c<em>click()<\/em>\u201d method to select a radio button (see line#35)<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473701a78\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3567\" data-css=\"tve-u-19473702b81\" data-height=\"280\" data-id=\"3567\" data-init-height=\"624\" data-init-width=\"1174\" data-width=\"527\" decoding=\"async\" height=\"280\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.13.38\u202fPM.png\" style=\"aspect-ratio: auto 1174 \/ 624;\" title=\"Screenshot 2025-01-17 at 2.13.38\u202fPM\" width=\"527\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Execute. <br \/>Notice below that \u2018Radio3\u2019 is selected<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947370c62e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3568\" data-css=\"tve-u-1947370d6d2\" data-height=\"420\" data-id=\"3568\" data-init-height=\"958\" data-init-width=\"800\" data-width=\"351\" decoding=\"async\" height=\"420\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.14.21\u202fPM.png\" style=\"aspect-ratio: auto 800 \/ 958;\" title=\"Screenshot 2025-01-17 at 2.14.21\u202fPM\" width=\"351\"\/><\/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 radio button<\/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 radio button<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947371a736\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3569\" data-css=\"tve-u-1947371b761\" data-height=\"299\" data-id=\"3569\" data-init-height=\"688\" data-init-width=\"1184\" data-width=\"515\" decoding=\"async\" height=\"299\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.14.58\u202fPM.png\" style=\"aspect-ratio: auto 1184 \/ 688;\" title=\"Screenshot 2025-01-17 at 2.14.58\u202fPM\" width=\"515\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.\u00a0<\/p>\n<p>Notice below that \u2018Radio1\u2019 is selected<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473722b01\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3570\" data-css=\"tve-u-19473725318\" data-height=\"444\" data-id=\"3570\" data-init-height=\"894\" data-init-width=\"910\" data-width=\"452\" decoding=\"async\" height=\"444\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.15.50\u202fPM.png\" style=\"aspect-ratio: auto 910 \/ 894;\" title=\"Screenshot 2025-01-17 at 2.15.50\u202fPM\" width=\"452\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So this is how we can use the various methods to select a radio button.<\/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> Blog32_RadioButtons_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;[value=&#8217;radio1&#8242;]&#8221;);<\/p>\n<p>\/\/Method 2<\/p>\n<p>page.locator(&#8220;[value=&#8217;radio2&#8242;]&#8221;).check();<\/p>\n<p>\/\/Method 3<\/p>\n<p>page.click(&#8220;[value=&#8217;radio3&#8242;]&#8221;);<\/p>\n<p>\/\/Method 4<\/p>\n<p>page.locator(&#8220;[value=&#8217;radio1&#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 radio buttons. Topics that we will cover: \u201ccheck()\u201d method to select a radio button \u201clocator() method clubbed with check()\u201d method to select a radio button Use the &#8220;click()&#8221; function to pick a radio button \u201clocator() method clubbed with click()\u201d method to select a radio [&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-3559","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\/3559","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=3559"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3559\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}