{"id":3559,"date":"2025-01-17T08:39:24","date_gmt":"2025-01-17T08:39:24","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3559"},"modified":"2025-01-17T10:55:03","modified_gmt":"2025-01-17T10:55:03","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><em>In this blog we will be utilizing Playwright Java to handle radio buttons.<\/em><\/p>\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&nbsp;<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a>&nbsp;<\/p>\n<p>Observe below that none of the radio buttons are selected by default<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3562\" width=\"536\" data-init-width=\"1174\" height=\"259\" data-init-height=\"568\" title=\"Screenshot 2025-01-17 at 2.10.13\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.10.13\u202fPM.png\" data-width=\"536\" data-height=\"259\" style=\"aspect-ratio: auto 1174 \/ 568;\"><\/span><\/p>\n<p>&nbsp;Let us see how to select a radio button, for example \u2018Radio1\u2019.<\/p>\n<p>Let us inspect \u2018Radio1\u2019<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3563\" width=\"505\" data-init-width=\"1174\" height=\"337\" data-init-height=\"784\" title=\"Screenshot 2025-01-17 at 2.10.56\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.10.56\u202fPM.png\" data-width=\"505\" data-height=\"337\" style=\"aspect-ratio: auto 1174 \/ 784;\"><\/span><\/p>\n<p>&nbsp;Let us use \u201ccheck()\u201d method to select the radio button and pass the \u2018value\u2019 attribute (line#29)<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3564\" width=\"558\" data-init-width=\"1174\" height=\"336\" data-init-height=\"706\" title=\"Screenshot 2025-01-17 at 2.11.32\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.11.32\u202fPM.png\" data-width=\"558\" data-height=\"336\" style=\"aspect-ratio: auto 1174 \/ 706;\"><\/span><\/p>\n<p>&nbsp;Run the script.&nbsp;<\/p>\n<p>Notice below that \u201cRadio1\u201d gets selected<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3565\" width=\"476\" data-init-width=\"1174\" height=\"331\" data-init-height=\"816\" title=\"Screenshot 2025-01-17 at 2.12.00\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.12.00\u202fPM.png\" data-width=\"476\" data-height=\"331\" style=\"aspect-ratio: auto 1174 \/ 816;\"><\/span><\/p>\n<p>&nbsp;<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&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3566\" width=\"459\" data-init-width=\"1174\" height=\"552\" data-init-height=\"1412\" title=\"Screenshot 2025-01-17 at 2.12.29\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.12.29\u202fPM.png\" data-width=\"459\" data-height=\"552\" style=\"aspect-ratio: auto 1174 \/ 1412;\"><\/span><\/p>\n<p>&nbsp;<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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3567\" width=\"527\" data-init-width=\"1174\" height=\"280\" data-init-height=\"624\" title=\"Screenshot 2025-01-17 at 2.13.38\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.13.38\u202fPM.png\" data-width=\"527\" data-height=\"280\" style=\"aspect-ratio: auto 1174 \/ 624;\"><\/span><\/p>\n<p>Execute. Notice below that \u2018Radio3\u2019 is selected<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3568\" width=\"351\" data-init-width=\"800\" height=\"420\" data-init-height=\"958\" title=\"Screenshot 2025-01-17 at 2.14.21\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.14.21\u202fPM.png\" data-width=\"351\" data-height=\"420\" style=\"aspect-ratio: auto 800 \/ 958;\"><\/span><\/p>\n<p>&nbsp;<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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3569\" width=\"515\" data-init-width=\"1184\" height=\"299\" data-init-height=\"688\" title=\"Screenshot 2025-01-17 at 2.14.58\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.14.58\u202fPM.png\" data-width=\"515\" data-height=\"299\" style=\"aspect-ratio: auto 1184 \/ 688;\"><\/span><\/p>\n<p>&nbsp;Execute.&nbsp;<\/p>\n<p>Notice below that \u2018Radio1\u2019 is selected<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3570\" width=\"452\" data-init-width=\"910\" height=\"444\" data-init-height=\"894\" title=\"Screenshot 2025-01-17 at 2.15.50\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.15.50\u202fPM.png\" data-width=\"452\" data-height=\"444\" style=\"aspect-ratio: auto 910 \/ 894;\"><\/span><\/p>\n<p>&nbsp;So 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>\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>\n<p><strong>public<\/strong><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><strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/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><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><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","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":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3559","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\/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":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3559\/revisions"}],"predecessor-version":[{"id":3750,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3559\/revisions\/3750"}],"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=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}]}}