{"id":2727,"date":"2023-11-21T19:16:00","date_gmt":"2023-11-21T19:16:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2727"},"modified":"2023-11-19T16:27:24","modified_gmt":"2023-11-19T16:27:24","slug":"explicit-wait-in-selenium","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/21\/explicit-wait-in-selenium\/","title":{"rendered":"Explicit wait in Selenium\u00a0"},"content":{"rendered":"\n<p>1. Introduction of explicit wait in Selenium<\/p>\n\n\n\n<p>2. Practical usage of explict wait<\/p>\n\n\n\n<p>3. Source code<\/p>\n\n\n\n<p><strong>Introduction of explicit wait in Selenium<\/strong><\/p>\n\n\n\n<p>In the previous blog, we have studied about implicit wait, the default wait time selenium waits to find elements on web page.&nbsp;<\/p>\n\n\n\n<p>Sometimes we may want to wait extra time only for a certain element. For example, we might explicitly want to wait for 10 seconds before clicking a button, but we may set the implicit timeout of 5 seconds for all the other elements on the webpage.<\/p>\n\n\n\n<p>Unlike implicit wait, the explicit wait is not a global timeout. Thus explicit wait is set for a specific object on the webpage. If there are 50 elements on a webpage and if a single element takes a long time to load, we can apply explicit wait for that specific element.&nbsp;<\/p>\n\n\n\n<p>Just for a single element, the implicit timeout (global timeout) should not be changed.&nbsp;<\/p>\n\n\n\n<p>Let us see how to setup explicit wait for a web element.&nbsp;<\/p>\n\n\n\n<p><strong>Practical usage of explict wait<\/strong><\/p>\n\n\n\n<p>Let us go to <a href=\"http:\/\/uitestingplayground.com\/ajax\" target=\"_blank\" rel=\"noopener\">http:\/\/uitestingplayground.com\/ajax<\/a>&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/XHXKRIQRoDl5OB2V7MbFpOGNJc13kXnsjkRoi-GHxptb7HFhPZarjezTGpbWDVgMk6kvotXEZMwyMXwaU0HzdfW_67e-BZqLbzrquMJvno5JfEmiBCgCgILUQvqLW7Iml4kRbbKl5Zz2sB0SRLsf\" alt=\"\"\/><\/figure>\n\n\n\n<p>The highlighted descrition (see above) says that when we press the blue colored button, the ajax text would appear after 15 secs.<\/p>\n\n\n\n<p>Let us manually click the button, notice that the ajax data loads after 15 sec&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/frNTEiY6hXidE9mCT6wwSJY_43mzJ2nvyHqPEqMTVAVxFUoBkgq89PJqPDEM6hHi2froWUSAuuDBBWrr8rUczflcycoVwt6G6vhJK4oEkBc9S8V64HuN2REzYhCxOwh0GjxQjlN4kPsDGH4cZEoD\" alt=\"\"\/><\/figure>\n\n\n\n<p>Inspect the blue colored button<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Vi9dtiIby3f03RAlqecYY3dAJMy-mTRi_BO1qwPX6mdAwowKl9KRaQIXqxHu5XpI6vyKOviXD3nomWMAzKWGkUr2sQIpnwCMAH61xY4TZT5OVD5CwssdtXvEZGxBr06YCc8BAzLBDmK5JO-2IjE1\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can make use of xpath to identify the button<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/h0iFd7RgfeqTD2FiRMoGmJ1qqz7qovqD-RBXyxEjUtj6NSDR6bj4_LLpPGbmtD981qqcRSvZMPtsff4efzGlfnC2zZc3pQgfqvMcHRsNd2z6kcQv14Db_iWKfLchoEuVLlhUeuQMUs5qgvgG1mDN\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us now inspect the AJAX data&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/JYupnjCiGu0Deb8bGligVkdNNzFsl2W8tqsRjyX5JfKan-v9_l-CMvmb3eAaaEcDd8xQ0WIpxmQ7LAwZzFD9jmjPbV96K79YytpnHcNLrcMvNmho8oj3whh8Gp0ZIZX0vCzuaDdXevsRmmGpP2ib\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can make use of xpath to identify the ajax output.&nbsp;<\/p>\n\n\n\n<p>Let us catch the text of ajax output in the string variable and try to print it<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/7DjHp8ykZvcFYGhnbcz7C64AGd4sYbUeklwzxld4Dq_3tj6Nlwy4pKoSeW3RdD1Lnqm2h5FCG7hkSajR1xhxMes45Djp7QBc_soVaeIpqnp1pOn3RpVs_7hvndtYgyFRVVnXBKguZXuK4tUT8Tb6\" alt=\"\"\/><\/figure>\n\n\n\n<p>The code so far:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/W6cEut933AMxBiSiSInIwDC02d5wzADuNp4dI86aRekCF9xEM-m7GEVxaYAeszcTbgBg9QK3LqYlp3uqf8-ozSWFKB0T9TYysyyAu_2DFsOK9T6H9SFqyO0luYkV0GtaoWjuHIS1hIjMQq5kBdvI\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute test&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/mDS6xMtsUPhHgksguiT3QG7vX9mvIT0g5u2aT8mrIiykElUIiHdhc1eSm5vZdKuq52YwZwi-UX1cd66ghP7YegBvNKQOBvtC7Y1M-LmIARHYDvKOvj-Scaw16Ux-7CRSSCuPf9BgCKA0qO7lGLjD\" alt=\"\"\/><\/figure>\n\n\n\n<p>The test fails and \u2018NoSuchElementException\u2019 is thrown (see above).&nbsp;<\/p>\n\n\n\n<p>To resolve this exception, we will set the explicit wait timeout for only the above specific element. So, before fetching the innertext (line#21), we will explicitly wait for the ajax output&nbsp; <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/JNBRsLcWGke33bz2XKqBA8KVOyHN9Czbdec3AnPt_INKx6XNdc9VGQbjmkwajbjkzcBieuzvqixzv3dpUMWC9UwmMM_lYBfT7VnEp4ROnqYcT2wXqMbZE3Yqtc8w5umLFjDISTSDyQCw_ehx41ih\" width=\"286\" height=\"34\"><\/p>\n\n\n\n<p>Once the output is displayed on the webpage, the innertext will be fetched.&nbsp;<\/p>\n\n\n\n<p>Explicit wait can be implemented using inbuilt \u2018<strong>WebDriverWait<\/strong>\u2019 class.<\/p>\n\n\n\n<p>Before line#23 (see below), we define \u2018<em>WebDriverWait<\/em>\u2019 class having reference \u2018<strong>w<\/strong>\u2019. This reference will wait upto 15 seconds maximum for an element, after that it will timeout<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/SMDBuOjLjV-bpE6QxsQO0YiQy3AWHYI7jjzFiN3TP6Aso6Zx1x1J65D5hxRHAZUt6WYgvaSAPyt2h7lwS3pLVi6MX0mbL9VCaxHFjHw9W2GBjmXfLkArvaWnY8uAO4WbVIuQIw3GKqcrVVQt5HRd\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now we can use \u2018until\u2019 method along with various conditions on this reference. As an example, <strong><em>visibilityOfElementLocated()<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/RZemVrgluIVP3ywALXZbbhzb_wWK0w4nldInGaVaEth3H5aQot_wG28uihaR0dMvdhnBK6TJdE5R8FNnfvbfy1GFrljYLM_-l6zADpp3_te7G-k5eYeTNLmWsnz4Ppt-0z1UdcWRUM4cwo5-IUMd\" alt=\"\"\/><\/figure>\n\n\n\n<p>So, due to line#23, Selenium will explicitly wait for max<em> 15 sec for the object to be visible<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/PKJoeildA-_Io8KviGXvprOsovXbKivW0hC8AUvQ9RPgt5DK6V8ZYHBIG6BbavhYp9ybMMYLpQXfYeYq-pp7S7Wd-mUey04nntZu9agcR_ZcNh5DwptUorh5gk0pmH56UZtrmfpv4FKEiqaoLo7l\" alt=\"\"\/><\/figure>\n\n\n\n<p>Once the element is visible, selenium will extract the text (line#25). Please note that the reference \u2018<strong>w<\/strong>\u2019<strong> <\/strong>only knows about the element mentioned in line#25, the reference does not have any knowledge about other elements on the webpage<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/aviOOTqB1OGeD9phaWvPihCymMWQspKIpji68OxCAeK2Iy3v5QMSWgPNDDXpc4Ji9P8AITcbC1hmc4W7hHgLytQIH7Zf7EKntfzooiGmDHSvP6MRhbyS8ElF0Vn4mHcpIrufm09s4AmS3X2-kqBy\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute the test<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/KwRGWSzrwe4rTzwsiZEgSUpbv2h9yQl8-Py3aVKBYLdfQvoYlPh76mdALvUm7QFECB6gjVsdil3ZgVAikmZi_Dggw-QPVP0WdZkn9eQu5eSyC2PUlqOHa4gPMXhGldk9snE6s1FaC9BvroKOklV9\" alt=\"\"\/><\/figure>\n\n\n\n<p>The ajax text gets printed in console<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/9PEHZ6ByVLC4dJoFVhdWuI1E5DJ87w62LiJNMlgrQDMcJguqYkSyErNKkn4bqnYNcydSKdGKIVt9X9pH6o5Rzdw8KbP3YctEdBkgab7J3oLZ6-cdPD2hR5dULhdM299xwHg8B9j3WAzjgQ--IDEJ\" alt=\"\"\/><\/figure>\n\n\n\n<p>This is the magic of explicit wait. It waits only for specific webelement on the page upto the duration of explicit wait time mentioned in the script.<\/p>\n\n\n\n<p>Let us comment line#21 and add line#22 to include 14 sec explicit wait time<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/5-zf2QK93Y_vNpx2P76_vrF3QzNq8EXClSoPWFa0P5IyxSep2E9gLSR9k8GibTtlL5Z4PFsErkyHEah6JLE6b3Z8YewENtPppfiqwPAN0AmdLHGBizVjXZwmz-ajZ28G2Rq-JKOx1CWoZ03SOKPX\" alt=\"\"\/><\/figure>\n\n\n\n<p>The script should fail this time since the timeout is 14 sec while the Ajax data appears after 15 seconds.<\/p>\n\n\n\n<p>Let\u2019s see and execute the test.&nbsp;<\/p>\n\n\n\n<p>As expected, we get \u2018TimeoutException\u2019<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ZV3i9-y0mcvreMexhviK_jqeWw-w3ek21HMzoH26v6NbQbD1a98UUEXk6hlrdPjK1oUxqlktnVwBoQ8i26G2aHgcf7yoB49IClDg0OlLDdc49o0heW304Z7dRmVyhlp0hyCjPecSuKxLLptFVdkd\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us comment line#22 and add line#23 to include 16 sec explicit wait time<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ba7GHX8QfQVYnYFS-j5UaE4Lh19Krbc85fPpnFGwJfAaYEgmuKWYTf7N23IVkTZXfceuSU-6UziFgnZWn54s-UcrwlIvzpzzT_2oDfxQS3CtlZtBkK9NCYglt6r9qhCmruDXc5XUG8CCTl0lWkzP\" alt=\"\"\/><\/figure>\n\n\n\n<p>The script should pass this time since the explicit wait is greater than 15 secs.<\/p>\n\n\n\n<p>Let\u2019s see and execute the test. The script passes<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Aahrk1HKzXOEss8HkLVseZsStqVq97GSUinnFMgG8Ai4jF_zVfsVPRWE3DtMcbcQhOm3AmEjwyrpBZDrEBP3IHyi1XGB2SChc4iffVp-WdvW3N6IEbHAabjfarKA9sUJfBdy4xrDn0dRro70NdRq\" alt=\"\"\/><\/figure>\n\n\n\n<p>This is how explicit wait works for a specific element.<\/p>\n\n\n\n<p><span style=\"text-decoration: underline;\">Code snippet\u00a0<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package Intro;\n\nimport java.time.Duration;\n\nimport org.openqa.selenium.By;\n\nimport org.openqa.selenium.WebDriver;\n\nimport org.openqa.selenium.firefox.FirefoxDriver;\n\nimport org.openqa.selenium.support.ui.ExpectedConditions;\n\nimport org.openqa.selenium.support.ui.WebDriverWait;\n\nimport io.github.bonigarcia.wdm.WebDriverManager;\n\npublic class Blog9_ExplicitWait {\n\npublic static void main(String&#91;] args) {\n\nWebDriverManager.firefoxdriver().setup();\n\nWebDriver driver = new FirefoxDriver();\n\ndriver.get(\"http:\/\/uitestingplayground.com\/ajax\");\n\ndriver.findElement(By.xpath(\"\/\/button&#91;@id='ajaxButton']\")).click();\n\n\/\/WebDriverWait w = new WebDriverWait(driver, Duration.ofSeconds(15)); \/\/success\n\n\/\/WebDriverWait w = new WebDriverWait(driver, Duration.ofSeconds(14)); \/\/fail\n\nWebDriverWait w = new WebDriverWait(driver, Duration.ofSeconds(16)); \/\/success\n\nw.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(\"\/\/p&#91;@class='bg-success']\")));\n\nString ajax_message =\u00a0 driver.findElement(By.xpath(\"\/\/p&#91;@class='bg-success']\")).getText();\n\nSystem.out.println(ajax_message);\n\n}\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction of explicit wait in Selenium 2. Practical usage of explict wait 3. Source code Introduction of explicit wait in Selenium In the previous blog, we have studied about implicit wait, the default wait time selenium waits to find elements on web page.&nbsp; Sometimes we may want to wait extra time only for a [&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-2727","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\/2727","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=2727"}],"version-history":[{"count":9,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2727\/revisions"}],"predecessor-version":[{"id":2794,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2727\/revisions\/2794"}],"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=2727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}