{"id":2759,"date":"2023-11-23T13:29:00","date_gmt":"2023-11-23T13:29:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2759"},"modified":"2023-11-19T16:29:44","modified_gmt":"2023-11-19T16:29:44","slug":"exceptions-in-selenium","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/23\/exceptions-in-selenium\/","title":{"rendered":"Exceptions in Selenium\u00a0"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>InvalidArgumentException<\/li>\n\n\n\n<li>UnhandledAlertException<\/li>\n\n\n\n<li>NoAlertPresentException<\/li>\n\n\n\n<li>InvalidSelectorException<\/li>\n\n\n\n<li>NoSuchElementException<\/li>\n\n\n\n<li>TimeoutException<\/li>\n\n\n\n<li>NoSuchSessionException<\/li>\n\n\n\n<li>ElementNotVisibleException<\/li>\n\n\n\n<li>StaleElementReferenceException<\/li>\n\n\n\n<li>Compound class names not permitted<\/li>\n\n\n\n<li>Element not clickable at point (yy, zz)<\/li>\n\n\n\n<li>NoSuchFrameException<\/li>\n\n\n\n<li>NoSuchWindowException<br><\/li>\n<\/ol>\n\n\n\n<p><strong>InvalidArgumentException<\/strong><strong><br><\/strong><em>InvalidArgumentException<\/em> is thrown when we do not include <em>https:\/\/<\/em> or <em>http:\/\/<\/em> in the url.<\/p>\n\n\n\n<p>Notice below, we get <em>InvalidArgumentException<\/em> since <em>https:\/\/<\/em> is missing in url<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/g9DzAT4mg5tb_faWkpY4HPpx8sYwYkghYzUrf5006aXRuYLVQ_VA-g4NYqUZYTI1vuxNoxVuF_O5ZvzzG8eWenbOx7Y4bRSI0j51En4fy9bAbRkOrDc4wqPW9IeluCprjI6WBk2m38Cs_uHxqhaS\" alt=\"\"\/><\/figure>\n\n\n\n<p>To resolve this exception, we have to simply include <em>https:\/\/<\/em> as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/fcI1oToa9JuFzQQmaQ49p3REbwW4-hiAUXcnpk-OFWjTOKgKPZobYPxULrsJYm0b3ccrFPliqELsrRJkkUAHdnwdpjaB_i7lb9PoZDiG3k7C1da4m8SpnJPQ4uZnOUBlNE2XpYI8qMdAGaQxyBr3\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now if you run, there is no exception and the website launches<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/KQSoY6Am91t6Z27am12ks2YTvUO_LJ21jGUWbT2XS0Z2CHoabVwLkP0tPPkYFjlkZCjC2_qtme29AEouxXZeIPjV-qqrlCKDOCF1D2BUPhbF2QdeZUBomO3tbjs4bWH-P5DTGFSRl6hRlGhRQmGV\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>UnhandledAlertException<\/strong><\/p>\n\n\n\n<p>We get <em>UnhandledAlertException<\/em> when we try to close the browser without accepting or dismissing the alert.&nbsp;<\/p>\n\n\n\n<p>So let us launch <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/fimta_r1-_ggiWRVpM6abPg0bgdibPzozYmdqrTFEbn5kkIjUInO_yJ3X-42IrlQH4UhC-OhIMMzkF3_6g7x41QH0MIMQRvL2ScB_t127d21KrPHsyCwbmNIn0OSf63sMyceI11GSWglNGCVo62j\" width=\"533\" height=\"25\"><\/p>\n\n\n\n<p>There is \u2018Confirmation Alert\u2019 button at the page bottom<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/QfHjCGRP11NCKIR4EhFM1Hf8-H1hbYfXWHLkUlBmVF6ucvezqi4sa3895NU6hAi7dB4P7JsVmQ3mXb9qwxEhE2fFqdRP-kjR8QqN10QYaq2CDfHRPKQGKJZiAB_lVYqS2xWmV76m1k167ozNu-pY\" width=\"577\" height=\"227\"><\/p>\n\n\n\n<p>Click the button, the alert popup comes up&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/BjbcYYgHXHKr6fBPwGT3L_mYeH1B0-3MfNmFgm5lmTsZYUGdRGoavG_mFkzWRIuPXkqe92dvJ1-DDk4AgOi-CB0MfzjLoabVUNd4gIHPcy55ISFr9-QU6SFw1dt-aAipm13IXP8KDoEZPrBkubTF\" alt=\"\"\/><\/figure>\n\n\n\n<p>If we try to close the browser without clicking \u2018OK\u2019, we will get <em>UnhandledAlertException<\/em><em>.<\/em><\/p>\n\n\n\n<p>See below, line#15 clicks the button and the alert pops up. Without accepting this alert, we are closing the browser (line#16)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/AQL7v4XyJpJAfFDjRLFX3TWiLz064NTMfs247mVnfXPBkf8kl5P5XN4KhUhoh1PuXeZLhH87DXlOhMmZbApYX7iP_TfQm7mv6skOmeDOVuhc0a93NEXi5aWaidsbw49Hi8y6fRIxcvPI78oSpjgH\" alt=\"\"\/><\/figure>\n\n\n\n<p>To resolve <em>UnhandledAlertException<\/em>, we have to simply accept the alert, see below, we don\u2019t get the exception now<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/VLtwO_nfy9dS_CnwMurp4F5s0ghP1XKqJfw6xHaH_x_QoppWeB-BqI76VcQ4FT6S9tPfeLXgA78BTXoLTklm1L3tCjq5ZtdyONegrh_m3aqhF_NC12f4FNuGgAaJWrv53FbnXJdrSShNrZihRfj8\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>NoAlertPresentException<\/strong><\/p>\n\n\n\n<p><em>NoAlertPresentException<\/em> is thrown when selenium tries to accept or dismiss an alert that is not yet displayed.<\/p>\n\n\n\n<p>To resolve this, selenium should wait for the alert to display.<\/p>\n\n\n\n<p><strong>InvalidSelectorException<\/strong><\/p>\n\n\n\n<p><em>InvalidSelectorException<\/em> is thrown when there is a syntax error in xpath.<\/p>\n\n\n\n<p>See below, we have an extra closing square bracket <strong>]<\/strong><\/p>\n\n\n\n<p>When we run the script, we get an exception<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ybjdzEeg3ertA5pVOfFieBetSzRt89Kak3_97VynhMBWAXdu3zIAp9KjZIFmrdbbGRWnRyyXRjb0P1-QhHN69j7b1uw7m52XJhtrc1tFPmrIlp895Js4OwSvHUHSDBGxSjjOM22L5A-_b_iq43Pl\" alt=\"\"\/><\/figure>\n\n\n\n<p>Simply correct the syntax to resolve the exception<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/_W4i-xV5yhY494OC665pA44dLCH9wHZRPN718ecKm57D7Ur0N_WxUSLuGkM1lHCf8cZtCVPn7zesQh7_3HdcYVoCOh_W193w064eAxc0vEAv5OmxZmv1qxh1wJkvw8W_TvECDVVAYU2bz3Fu581w\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now there is no exception and the page opens<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/RPN2jWK_esJmMfeZAr0tFgbIizo4T4HDcpRCnhPYOJ5R-hVCWNZhYNT8l_349_BWjPvdq8Cft0CdCItIIn2YjTv24I6IRnQYZIbPDxvFPQbuTZfDZjXpNvdXUs4xz99vSsXB1lU_KOC9EiJ5UZ92\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>NoSuchElementException<\/strong><\/p>\n\n\n\n<p>This exception is thrown when the element is not found on the webpage. In the below case, the xpath is incorrect and hence Selenium is not able to locate the element&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/HmI9GDCMII4zdPwVXR6y7l-vZSkdW-29kSID4hkzVAfob1QNX6YuiKYHG_CmA0JdLPHGXXSzXpMBbrJFIAaePqYYNjkq1pNVvY5o_XxgIuWIbYCeg1GF_zmdz3tI7g5L097kw11h4TRmG_pfR5rt\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>TimeoutException<\/strong><\/p>\n\n\n\n<p>Selenium, by default, waits indefinitely for a website page to load. However, we can instruct selenium to load the page within \u2018t\u2019 seconds.&nbsp;<\/p>\n\n\n\n<p><em>TimeoutException<\/em> is thrown if the page does not load within \u2018t\u2019 seconds.<\/p>\n\n\n\n<p>In the example below, we have set the page load timeout limit of 3 seconds. Notice that we get an exception, since the website page did not load within 3 seconds&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/g_bOfaBOtg6bgJc9KGEv5BAXyvQEaR33lTV4gRv-tYGvvs60ghhJ8ILBRJqm7fnlinE7WH6R3Hy7CUuS-6wOQPcriT4jyrWZeuEt0eQrcGvYLmPmB7Uklr2sNX2cGsWohcTWnyFkyFYpUTWMDNgY\" alt=\"\"\/><\/figure>\n\n\n\n<p>To resolve this exception, you can increase the page load timeout.&nbsp;<\/p>\n\n\n\n<p><strong>NoSuchSessionException<\/strong><\/p>\n\n\n\n<p><em>NoSuchSessionException<\/em> occurs when we try to perform an operation on a closed browser session<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/OI2DuIUslPlvgbG8AYcYiLxGokbKOgiHqyLh54ZYn4oKFroBK5bpS2HHBkxlii-rGWmsM0ncVbuUVevMML2zW07P5xhgQNKImwk3S-jqnCQZzBczrPTrnKbkt8s_4gRiLkTkh9BaSCCSm29K52hb\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>ElementNotVisibleException<\/strong><\/p>\n\n\n\n<p><em>ElementNotVisibleException<\/em> is thrown when an object is present in DOM (document object model) but it is hidden on the web page. This exception can also occur if the object is not properly loaded.&nbsp; Thus, if selenium tries to perform an operation on hidden element, ElementNotVisibleException will be thrown.<\/p>\n\n\n\n<p><strong>StaleElementReferenceException<\/strong><\/p>\n\n\n\n<p>In line#17, we are creating a reference to the text area.&nbsp;<\/p>\n\n\n\n<p>In line#18, we are refreshing the page and in line#19 we are trying to use the reference we created in line#17 which is no longer there (the reference becomes stale since the page is refreshed). Thus we get&nbsp; <em>StaleElementReferenceException<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/krB1gLWDNd8KQg-Nkzx8AigHrmyJJA2b5Qh37DkLdMIrLPC_vKAKAxEZVPnXHq2QQRh0Nej0UsmX7KgbdExWaeLLf3mZhBKschZlv_bfhAkSlILZsoA2N8r3jEhlBncR0aibr1o9vOWSHzqFfKNC\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Compound class names not permitted<\/strong><\/p>\n\n\n\n<p>Notice below in line#16, there is a space between 2 classnames. In this scenario, we will get \u2018Compound class names not permitted\u2019 error<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/xuGNgYVj0OS0-FB8xC3KRiKRjYMlPHCdpUlESk5jhc97rO0Ctf9Sqbcgcifk1Oiiuk78oiSoR97v5uQccRUYI81cMeAjxjwAv9PrdEzIeU_zUS2Jznw5CwHMDj8iyHP7XGms-dQnjsVOs-WdpyVp\" alt=\"\"\/><\/figure>\n\n\n\n<p>To resolve this, we should replace spaces with <strong>.<\/strong> within classnames, example <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/AvwAU6L9Aj39wQt3V7hoqNSbWNmODD8tBEFt6eP6DBsUm092pG8odNJqrf0Ra2qrg6K0P-yYGSw27Kfu5wMcPrKMDMhv7QadUMQlN1f4IKv2SleNQBbU1ivELrUmHzDCRmDa7aJxDifmpVLS_r01\" width=\"363\" height=\"34\"><\/p>\n\n\n\n<p><strong>Element not clickable at point (yy, zz)<\/strong><\/p>\n\n\n\n<p>This error occurs when the element is not at the expected exact location. Due to this, selenium does not click the element.<\/p>\n\n\n\n<p><strong>NoSuchFrameException<\/strong><\/p>\n\n\n\n<p><em>NoSuchFrameException<\/em> is thrown when selenium is not able to find the desired frame<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/PITY46vSlT4IwNpMTC0p9x3GkwhDdS6xQljiLBhY95I10P4k3bIuOPaDyi5Js12Ixg2nfA5HBhkbeTz-w9ZDznWDniw0-RnqTwchUIJhA-UvgrZQ_Ed4ipxG_esY0AirDdaKEt-mYcfx7P2f_jp7\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>NoSuchWindowException<\/strong><\/p>\n\n\n\n<p><em>NoSuchWindowException<\/em> is thrown when selenium is not able to find the desired window<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/JfjJjJiulLYv2zq0Gra-x356ptRURaytHfC4G9av13b6MfIk_EaM7ODtHBzMaq2WfthXDfUPkyQDedsufWtgvs12-kYYkkKJn05tq2rUvIunpVom2z5Yf4rmHPWyXtqOMu4wXZpUZhqRGnuS5IKY\" alt=\"\"\/><\/figure>\n\n\n\n<p>So these were the various exceptions in selenium.<\/p>\n\n\n\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>InvalidArgumentExceptionInvalidArgumentException is thrown when we do not include https:\/\/ or http:\/\/ in the url. Notice below, we get InvalidArgumentException since https:\/\/ is missing in url To resolve this exception, we have to simply include https:\/\/ as shown below Now if you run, there is no exception and the website launches UnhandledAlertException We get UnhandledAlertException when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1238,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-2759","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2759","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=2759"}],"version-history":[{"count":9,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2759\/revisions"}],"predecessor-version":[{"id":2796,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2759\/revisions\/2796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1238"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}