{"id":3459,"date":"2025-01-16T18:55:58","date_gmt":"2025-01-16T18:55:58","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/javascript-dialogs-handling-using-playwright-java\/"},"modified":"2025-01-16T18:55:58","modified_gmt":"2025-01-16T18:55:58","slug":"javascript-dialogs-handling-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/javascript-dialogs-handling-using-playwright-java\/","title":{"rendered":"Javascript dialogs handling 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=\"3459\" 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 javascript dialogs (alerts, popups, prompts).<\/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>Handle Javascript dialogs (example 1)<\/li>\n<li>Handle Javascript dialogs (example 2)<\/li>\n<li>Source code (example 1)<\/li>\n<li>Source code (example 2)<\/li>\n<\/ul>\n<p><strong>Handle Javascript dialogs (example 1)<\/strong><\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/the-internet.herokuapp.com\/javascript_alerts\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/the-internet.herokuapp.com\/javascript_alerts<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194707a98cb\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3462\" data-css=\"tve-u-194707ab053\" data-height=\"354\" data-id=\"3462\" data-init-height=\"842\" data-init-width=\"1198\" data-width=\"504\" decoding=\"async\" height=\"354\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.27.04\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 842;\" title=\"Screenshot 2025-01-17 at 12.27.04\u202fAM\" width=\"504\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>To handle this, we can add \u201c<strong><em>onDialog<\/em><\/strong>\u201d listener<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194707b766e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3463\" data-css=\"tve-u-194707baa2a\" data-height=\"268\" data-id=\"3463\" data-init-height=\"594\" data-init-width=\"1198\" data-width=\"540\" decoding=\"async\" height=\"268\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.28.23\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 594;\" title=\"Screenshot 2025-01-17 at 12.28.23\u202fAM\" width=\"540\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Whenever the javascript dialog\/alert appears, this particular \u201c<strong><em>onDialog<\/em><\/strong>\u201d listener will be invoked.<\/p>\n<p>In line#18, we have passed the lambda function inside the method parameter.<\/p>\n<p>In line#26, we are clicking the \u2018Ok\u2019 button by calling the accept method.\u00a0<\/p>\n<p>In line#28, we are printing the message on the dialog box.<\/p>\n<p>In line#31, we are clicking the button to open the alert dialog. Notice that we have placed this after the listener code. So it does not matter where we keep the \u2018click\u2019 method. The listener keeps listening for any alert.<\/p>\n<p>Execute.<\/p>\n<p>Notice below that alert comes up<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194707cdc9d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3464\" data-css=\"tve-u-194707cfbe5\" data-height=\"588\" data-id=\"3464\" data-init-height=\"1332\" data-init-width=\"1198\" data-width=\"529\" decoding=\"async\" height=\"588\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.29.26\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1332;\" title=\"Screenshot 2025-01-17 at 12.29.26\u202fAM\" width=\"529\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>We add the lines#32 and 33.\u00a0<\/p>\n<p>Rest of the logic would remain same<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194707db52d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3465\" data-css=\"tve-u-194707dcfbb\" data-height=\"569\" data-id=\"3465\" data-init-height=\"1234\" data-init-width=\"1198\" data-width=\"552\" decoding=\"async\" height=\"569\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.30.37\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1234;\" title=\"Screenshot 2025-01-17 at 12.30.37\u202fAM\" width=\"552\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Handle Javascript dialogs (example 2)<\/strong><\/p>\n<p>Let us see another example, go to <a class=\"\" href=\"https:\/\/mail.rediff.com\/cgi-bin\/login.cgi\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/mail.rediff.com\/cgi-bin\/login.cgi<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194707e59bf\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3466\" data-css=\"tve-u-194707e6de8\" data-height=\"333\" data-id=\"3466\" data-init-height=\"766\" data-init-width=\"1198\" data-width=\"521\" decoding=\"async\" height=\"333\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.31.28\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 766;\" title=\"Screenshot 2025-01-17 at 12.31.28\u202fAM\" width=\"521\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The below code will click the \u2018Ok\u2019 button and will capture the text of this alert box<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194707ed304\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3467\" data-css=\"tve-u-194707ef66d\" data-height=\"313\" data-id=\"3467\" data-init-height=\"688\" data-init-width=\"1198\" data-width=\"545\" decoding=\"async\" height=\"313\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.32.04\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 688;\" title=\"Screenshot 2025-01-17 at 12.32.04\u202fAM\" width=\"545\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>The popup comes up<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194707fb48e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3468\" data-css=\"tve-u-194707fcd03\" data-height=\"342\" data-id=\"3468\" data-init-height=\"716\" data-init-width=\"1198\" data-width=\"573\" decoding=\"async\" height=\"342\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.32.53\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 716;\" title=\"Screenshot 2025-01-17 at 12.32.53\u202fAM\" width=\"573\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Source code (example 1)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/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.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> Blog26_JSDialogs_Example1 {<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p><\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/javascript_alerts&#8221;);<\/p>\n<p>\/\/JS Alert, confirm, prompt<\/p>\n<p>page.onDialog(dialog -&gt; {<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(3000);<\/p>\n<p>}<strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>dialog.accept();<\/p>\n<p><\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(dialog.message());<\/p>\n<p>});<\/p>\n<p><\/p>\n<p>page.locator(&#8220;text=Click for JS Alert&#8221;).click();<\/p>\n<p>page.locator(&#8220;text=Click for JS Confirm&#8221;).click();<\/p>\n<p>page.locator(&#8220;text=Click for JS Prompt&#8221;).click();<\/p>\n<p><\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (example 2)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/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.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> Blog26_JSDialogs_Example2 {<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p><\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/mail.rediff.com\/cgi-bin\/login.cgi&#8221;);<\/p>\n<p>\/\/JS Alert<\/p>\n<p>page.onDialog(dialog -&gt; {<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(3000);<\/p>\n<p>}<strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>dialog.accept();<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(dialog.message());<\/p>\n<p>});<\/p>\n<p>page.locator(&#8220;text=Sign in&#8221;).click();<\/p>\n<p><\/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 be utilizing Playwright Java to handle javascript dialogs (alerts, popups, prompts). Topics that we will cover: Handle Javascript dialogs (example 1) Handle Javascript dialogs (example 2) Source code (example 1) Source code (example 2) Handle Javascript dialogs (example 1) Go to https:\/\/the-internet.herokuapp.com\/javascript_alerts\u00a0 To handle this, we can add \u201conDialog\u201d listener [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-3459","post","type-post","status-publish","format-standard","hentry","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3459","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=3459"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3459\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}