{"id":2714,"date":"2023-11-10T18:36:23","date_gmt":"2023-11-10T18:36:23","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/10\/java-alert-popups-and-prompt\/"},"modified":"2023-11-10T18:36:23","modified_gmt":"2023-11-10T18:36:23","slug":"java-alert-popups-and-prompt","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/2023\/11\/10\/java-alert-popups-and-prompt\/","title":{"rendered":"Java alert popups and prompt"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<p>1. Java alerts\/popups handling using Selenium WD (getText(), accept() methods)<br \/>2. Another example of JS Alert<br \/>3. Example of JS Confirm<br \/>4. Example of JS Prompt<\/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>Java alerts\/popups handling using Selenium WD (getText(), accept() methods)<\/strong><\/p>\n<p>Launch <img decoding=\"async\" height=\"25\" loading=\"lazy\" src=\"https:\/\/lh7-us.googleusercontent.com\/HYyjVQXLrYFfa9tVIm8rY6FcNIOlc7tZe0NdxWW6i98OsuGM7X8duE7zYellrb5l-5fTSDb7cfmNLF_Ex9YZmMSekZ1Qfp6ETzVghPCbE_Pyp-VGT4ce07bUgYmeuWZdVFLfmWUQnWBObWvVCIpP\" width=\"533\"\/><\/p>\n<p>Scroll at the page bottom. You will see \u2018Display\u2019 section having \u2018Confirmation Alert\u2019 button<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/rwxsbbuN8Xec_TVT_paCK3zf0yyerbdZJMN0TDNSBKXhBbjk3lNXmaGKcjdDhouf3DBgtJLr8OzEi7f8ehiQnwvQWzQ7kS3jtnAg-B8wEYsK8SjKK7MurVrwZQWCgMvwFRrn7n_TxGWAkVG0tTWI\"\/><\/figure>\n<p>Click \u2018Confirmation Alert\u2019 button to popup a message<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/R2iMIxE34YfDF899tG2_WTjG6_GlUidl0jhLMfMBz516fmhHmh6s1LxqkN1HxRjgFBiDUi684y8c2XBiNPf452zJRsh23-tnNhMSmhef714eLu4oITZ1nQVZ-RXePMJjYKk3uylmMfwUBmfCFRzi\"\/><\/figure>\n<p>We cannot right click on this popup alert. The reason being, this popup is a java alert popup and it is not web related. Since it not web related, we cannot inspect this popup by right click. To identify this popup, we will have to switch the driver from webpage to popup using \u2018Alert\u2019 class. However note that you should use this \u2018Alert\u2019 class only if it java alert, not always.\u00a0<\/p>\n<p>\u2018Alert\u2019 is an interface &amp; you can read the api documentation here <img decoding=\"async\" height=\"63\" loading=\"lazy\" src=\"https:\/\/lh7-us.googleusercontent.com\/HtriI8IMMuAedmMCuwkHWttwK8TBvGsaV3oDMZ76bSoMxlaQsMlOjadXOT1M69iglZIgIwCwXMKvaCvX86YT-0ngHfwnkxC8JUOKeA9wcbxAmvAx5TKoJNqNnpftO0B8aeBjcgbnXaG4LMn3yHmj\" width=\"599\"\/> Notice below that this interface has methods such as accept(), dismiss(), getText() etc<br \/><img decoding=\"async\" height=\"385\" loading=\"lazy\" src=\"https:\/\/lh7-us.googleusercontent.com\/sNUHMFZcWrUGnPpwdgyEVtrb4bR-cfuAWMwgp2lyoN7ZO-mq3QV1mKqRB9_5wAMGSTfU2uM4IRVnLjkurHLUyMX0r0ijXpMjZYxwMFtdkgT_OUtXr9ecB5LJdovQ0kX-o5GP3QLe7eBkGyx2d10H\" width=\"342\"\/><br \/>Before we perform any action on java alert popups, we have to first switch to the alert popup using the syntax: <strong><em>switchTo().alert().accept()<\/em><\/strong><\/p>\n<p>Let us inspect \u2018Confirmation Alert\u2019, it is an \u2018input\u2019 type button<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/fUwV_3gSkeST9LABO7ufnFhYUC_o1xadw2LJHvaWK0GK0YAJh-1_ShEkRav03nrLh-UCm4bY37Tsqiv_oE9mvA9v2EPDizXIMZsBHqtCP5zPGg_swYvBVOHOIKvnFXYJW7CGolvqWpCdQyD9FlR6\"\/><\/figure>\n<p>In line#15, we are clicking the \u2018Confirmation Alert\u2019 button\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/YxTWyyCj0wQvzVjpO8fl6gS1r5wZZ9-7f-hx2yn3mg0Qlxx3MfsdomLrd_hkKdm1UQxLyWezB8n8ul-RSRMNO9XZ5tcD083bkBlLJuNGzFQYpbdDbyzYKTslF2lgE2Q9jd8cGIgvkARST0jTaCs6\"\/><\/figure>\n<p>Let us run the script, notice below that the popup comes up<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/MGHlky_-AgNML16hIFtr4-oHN5biKX6ipSopRX54-zcECuRfb6yTQ9EEKX_cy7oIFVQDzpAI_wpr-YfvACWRujDtqmRWqAcEdKlgbl2WMtAfYVEHgvky2PCNQASLLhaZ_7cQ0C2_X5QwRqYgg_gB\"\/><\/figure>\n<p>We will now switch to this alert popup (line#16) and print the popup text using <em>getText()<\/em> method<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/OqNY90r6FswYZIXyaMHwrbC9Q0tf9eBkAftuYCDsbhw2U8cbCgzh9KjsFfAI-8teY1JQCY0pWs8Vr-T3izAfvE7l-SHV9YJqf0runUA_05ptbt_Pcjr35BjvXDkmt55I0PSAGHMfYsg44opdoZQx\"\/><\/figure>\n<p>Run the script, notice below that the text gets printed on the console<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/j5bKwzHYjERBsm0rFvhyxk9oosbfHNNzjV5zG6nFZ6qNPC32_ztz2gVR90N01oFG-k0nKsPCBFHb7ABUj7fvcB40YGaefZPX0lHAoV-BRVMGy_UquyfPnYExKBS3PJk6Ccre0Hx0MAQsdpOzIkT5\"\/><\/figure>\n<p>Using accept() method, selenium clicks the \u2018Ok\u2019 button on the popup (line#17)\u00a0<\/p>\n<p><img decoding=\"async\" height=\"107\" loading=\"lazy\" src=\"https:\/\/lh7-us.googleusercontent.com\/Hwbsw6JfQyqcOUtQWrE1a-j3MLnkWniNkfNY91_OH8g3SI02GeMwKkiOZpaLSBigQ0Cw6PahkmpvHBslBBOc1-1KUZpBkULjKEnEB25oacj_mGv7RkoFWyZylXUKm5qixrTlokpO55TezruaWtTW\" width=\"660\"\/><\/p>\n<p>Run the script, you will see the alert popup getting closed<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/kb2ehvRjevXRfvvxliszOhglrrSaqW8Iarw0uBwyWqfXCkaYD069MDVLHpLRlzwh9PeWHUU3MI6AUILpkrrMQuQjUXRCnIqEGxz2P42RD7WXT61HBmdsS9-47gMDLNafHYrGBoqzOd1BVELabrQs\"\/><\/figure>\n<p><strong>Another example of JS Alert<\/strong><\/p>\n<p>Similarly, let us launch<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/fD0rf-nw_f8oVqm5EbwhfifWTJjW0zYxDsnQQJ4g7ZrZKFarI7tO0ZEV93o2lwysaDEVHXsBhzVdPxm_vFDn0jlhf9QDyvSulyM1TceDtI5qYGSB2U1NocdeV_xb4MNe4bjFah9t7FtPiXJCZDCa\"\/><\/figure>\n<p>Click \u201cClick for JS Alert\u201d, the below popup would come up<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/D3cpgiqpLu-uyJHdit40vyD26Oh97ubr3LrWHJndBokFTXrcbnnsbp4lFuLi8XOZYyO7mpXO4JVqu3E4ouDreXWBW6U4uDQ5fPjWlhgzr4SYih0M1WLCP5cfdTzC3_DNEBkH_lC78cqiXppPHLgM\"\/><\/figure>\n<p>Let us use the same logic to extract the text and accept the popup<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Fkv21LnNdPpPRk5p9PkpAzNn2tiR4QKtQ0JjaG2q8zOF-WvkGWYvi507jlaTA3-1pKhyjl6QGFjq4luZ-6rpq54OGAyjJpd4WODqo9xChO0d2lRO8-5-FGRGV4znL54T4txhNyT0xpmatRHEswO-\"\/><\/figure>\n<p>Run the script, the text message is printed in the console and the popup gets closed<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/BgCuSLTywW7-YAhb2P2INB5Y38-qgFVZXGwx1AJFes4TmAjJk3yVctuXkRdxCqRVaRKA6pKqne9-UOvK2cQs1t2CV_PdjQcz2Z62N-ZfG1kj_wGfCYc_kfdu5KmmxICrKivQ3-yIhtdW9neImLHu\"\/><\/figure>\n<p><strong>Example of JS Confirm<\/strong><\/p>\n<p>When we click \u2018Click for JS Confirm\u2019, we get a popup that has \u2018Cancel\u2019 button as well<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/OsBgv1y8-fqk69JBq1DPDwKZa1bVifkFMIdef0imFNyGc2MTKQ_gVzZKDuK-JeWX7mKEOyttXLEcsHGw72SklQuLU47zmWSxnmLjp4d_ztiwHmYpNYLXnFCTb_h90gKbFX0pChimuHFgDE6dIaeD\"\/><\/figure>\n<p>We can now use the <em>dismiss()<\/em> method to cancel and close the popup<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/FW4t3j1EKcWv7B8H-CG2vI4uid17xw3ezv4cp6RcWQaJUUG5XIhbAs2YZtJ1jnfopG3XCvqM4loHvpETP5J7QJAi36hpH8eJz-BWN7MFHQKh6Qnfrf8ILcI6BHgx4mRYolMEZDRHPrkLAAxnYah6\"\/><\/figure>\n<p>Run the script, the text gets printed in console and the popup closes<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Ab3C-UchixesaRENvmpMVzsagaw-UdnmnM64jKDoV_fdswzTRuWsHI8HIaZ_0hk-INofuHJHh3KtiXICaMXBVYBiw4YvI5H0cMmEasFPmL20qJBEpqFYGG8vdc5fZqXrdJeS9pluX6apVCY4uS8U\"\/><\/figure>\n<p><strong>Example of JS Prompt<\/strong><\/p>\n<p>Next, let us look at \u201cJSPrompt\u201d, when we click this, we get a prompt having a text field wherein we can enter some text<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/rPHFniim-mcxVrECjre13e5U9QzB2J1c4NqsR5H5mIccX3Hh1zw7AhuQu5vlsAu8FRODwigwkD-Hi1X7aapUahBvGIVc74X1hGI7StZhIEp8ZHATf-0HqZh2r_joYN6UksEaqgMc94VMhjaPTa6C\"\/><\/figure>\n<p>The below code does the same<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/usCXTFfBkzNw1smd7ZPtTCmlIucBnkV0LKodKAKdNKQkab64pDtTRfDh6sIw-oxF51ltSPLGRco2VqBXtWllwgB9v1XP5IZxJDlGH-AXmrKTzCl3Nex_ME6HfkJ2mbmYIC8pMNMNk6mMouxma2aa\"\/><\/figure>\n<p>Run the script, notice below that the text gets typed in the text field<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/gWOFT-ygc0KeONFxE24Ij1WAMvirCW1-x6mD1Lg70fkm7G0_VDMA8Q3XTH4lrTX0-ksT9dUB29r8RN8htsuS2VKVk14BYgHor9iMqG7v6wdxilp9IdylQUWBWCjbovd06guYR4AhdZpDkArEM4oX\"\/><\/figure>\n<p>Also the text on the popup gets printed in console<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Efr6s_4GHcP5_H425MclCUs9zyeG_xc8Bl9hcQNjJR37XItCvY55Qiz7IhHLJEe57vedi8DQMakkxoMaxtt-EOoSRioDJVW2MqnDwAlWu01q6bVzEyfuV-K8kbMyODG9RsW8TUoxlASQvMLAQGbr\"\/><\/figure>\n<p>To close the popup, we can simply use the <em>accept()<\/em> method<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/09lYSemeOhXmbsUeP1EWaq-stH-kW3JfT93dxvjdGkAaZ-LlveNCeqYx2i3gwZZ672IYa6VT0qft6KaNVlFRZPemkKesbgxYPJHKDiJ1D2xzDdRgBjAZSqGEDFyfAwbskSzINi3K86qkz1vp0qhE\"\/><\/figure>\n<p>Run the script, the text that we entered in the text field gets shown in the \u2018Result\u2019 section<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/lfIL76E70msYq_XO1dDGpTSubVdBV5_V6iED8qY_QbODOO8RcYnEU6zWt26JsaaACc5gPGBrCEAnZBg9xoRA-9W9fSIy3nDNWzomV8PIi5wW73AhFCDtOZSYjmcoIm-UhRiCdHh2jBYdpBCBjeTB\"\/><\/figure>\n<p>We can put a simple validation as shown below<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/HIcLQDHXCW5uY0ziE8_nbTUcXzZvMc3RSF7P3-GecsqbqOEJbBeBsLz5k4JgDPHFWu1myOBZd4MlyJDBes1BHuENfxQczWCS0aREzSzWsLPisQ-xp31jjAr9ZWLXMugJvy-VO10feoj1KMQWHXrp\"\/><\/figure>\n<p>Run the script, notice that the SOP message gets printed in the console<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/I2rLt4dqoxkHs_M_4a9dpsrUw4R0hAwhOl0m_1HJFvFOvcdskUPGd42gsFMYgeVgzFBIyJ07v3GyS0ABGLxwjcS9zuO4oFlkaSOV4SZfGDJvdXFcPX-Ft5pkBdXI3Tymu_Gem6xyRV1zuRZr-xCh\"\/><\/figure>\n<p>So this is how we can work with JS alerts and popups.<\/p>\n<p>Thank you.<\/p>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Java alerts\/popups handling using Selenium WD (getText(), accept() methods)2. Another example of JS Alert3. Example of JS Confirm4. Example of JS Prompt Java alerts\/popups handling using Selenium WD (getText(), accept() methods) Launch Scroll at the page bottom. You will see \u2018Display\u2019 section having \u2018Confirmation Alert\u2019 button Click \u2018Confirmation Alert\u2019 button to popup a message [&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],"tags":[],"class_list":["post-2714","post","type-post","status-publish","format-standard","hentry","category-java","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2714"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2714\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}