<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: nishantchoudhary123</title>
    <description>The latest articles on DEV Community by nishantchoudhary123 (@nishantchoudhary123).</description>
    <link>https://dev.to/nishantchoudhary123</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F903769%2F54bba55b-e69f-47e9-9118-daaa9cb2f5a7.png</url>
      <title>DEV Community: nishantchoudhary123</title>
      <link>https://dev.to/nishantchoudhary123</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nishantchoudhary123"/>
    <language>en</language>
    <item>
      <title>Use Selenium wait for page to load with Python [Tutorial]</title>
      <dc:creator>nishantchoudhary123</dc:creator>
      <pubDate>Fri, 05 Aug 2022 11:05:00 +0000</pubDate>
      <link>https://dev.to/testmuai/use-selenium-wait-for-page-to-load-with-python-tutorial-3ag7</link>
      <guid>https://dev.to/testmuai/use-selenium-wait-for-page-to-load-with-python-tutorial-3ag7</guid>
      <description>&lt;p&gt;One of the primary requisites to automate interactions with a WebElement in the DOM is that it should be visible and interactable. Like me, you would also come across several scenarios where your Selenium Python scripts threw an ElementNotVisibleException.&lt;/p&gt;

&lt;p&gt;The failure in the &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test automation&lt;/a&gt; script can be attributed to the presence of dynamic WebElements on the web page. The WebElement under test might not have been loaded on the web page and your test is trying to perform some activity on that WebElement. It is known that dynamic content loading with AJAX is widely used across different web products (or websites). When interacting with dynamic WebElements using Selenium test automation, it is recommended to add Selenium wait for the page to load, so that the element is available for performing tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hey, This tutorial on &lt;a href="https://www.lambdatest.com/blog/playwright-framework/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug04_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;playwright browser testing&lt;/a&gt; will guide you through the setup of the Playwright framework.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2ozlc0ob7do79wqhezk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2ozlc0ob7do79wqhezk.png" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Selenium wait in Python gives additional time for loading of the WebElements in the DOM. In this article, we deep dive into the different types of wait in &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; along with the usage of Selenium wait for page to load in Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Selenium Wait For Page To load?
&lt;/h2&gt;

&lt;p&gt;To answer this question, it is essential to understand the ‘where’ and ‘why’ of dynamic page loads. Some of the conditions mentioned below might be known to you or you might have already encountered them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 1: Uploading files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I am sure you might have uploaded some file, image or video on some online platform. You might have noticed that once you select the file, it takes some time to upload the same. ON similar lines, when you try to upload files using &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; test automation scripts, you will need to implement Selenium Wait in Python for realizing the successful uploading of the file. successfully. If you don’t use the Selenium wait for page to load after upload, you might witness some errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 2: Delayed confirmation message&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Applications like Gmail allow the users to interact and work on a real-time basis. Even though you are able to interact with the application through the email sending usecase, you do not get an immediate confirmation of the delivery. The confirmation depends upon a number of factors like network availability, attached file size, etc.&lt;/p&gt;

&lt;p&gt;As a QA engineer, we need to factor in such conditions when planning and performing usability and user acceptance tests. For Python, you will have to implement Selenium Wait for page to load in order to ensure that tests are performed with the necessary WebElements in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 3: Conditional load of Page Elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Certain websites have some components or elements hidden, or not visible at an initial stage. They can be interacted with only after some preset conditions are met. For example — On a movie ticketing website, the button for booking a seat becomes available only after some preset time. This is a classic case of conditional loading of page components. To handle such scenarios in test automation scripts, you will need to implement Selenium wait for page to load.&lt;/p&gt;

&lt;p&gt;Selenium wait for ensuring the page to load is applicable in other scenarios like skipping the ad in YouTube, &lt;a href="https://www.lambdatest.com/blog/how-to-lazy-load-images-javascript/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;lazy loading of images&lt;/a&gt; in webpages, and more.&lt;/p&gt;

&lt;p&gt;Start your free &lt;a href="https://www.lambdatest.com/python-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python automation testing&lt;/a&gt; today.&lt;/p&gt;

&lt;p&gt;This certification is for professionals looking to develop advanced, hands-on expertise in Selenium automation testing with Python and take their career to the next level.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/KeSWHW4Lx1E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Different Types of Python Selenium Wait
&lt;/h2&gt;

&lt;p&gt;Selenium WebDriver provides a “wait” package to deal with conditions where you need to wait before interacting with target WebElements. You can also leverage Python’s ‘Sleep’ function to wait for a specified interval, however, that approach is not a recommended one!&lt;/p&gt;

&lt;p&gt;There are three different ways to implement &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Wait&lt;/a&gt; in Python for page to load:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Explicit Waits&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implicit Waits&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fluent Waits&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug04_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium testing&lt;/a&gt;: Try this online Selenium Grid to run your browser automation testing scripts. Our cloud infrastructure has 3000+ desktop &amp;amp; mobile environments. Try for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explicit Waits in Selenium Python
&lt;/h2&gt;

&lt;p&gt;Explicit waits are introduced to temporarily freeze the execution of the Selenium test automation script. It makes use of the functions available in Selenium WebDriver’s wait package. The program halts the execution for a specified time or until a certain expected condition is fulfilled.&lt;/p&gt;

&lt;p&gt;Explicit waits can be implemented using the WebDriverWait class of &lt;a href="https://www.lambdatest.com/blog/getting-started-with-selenium-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium python&lt;/a&gt; bindings. Let’s take a look at the WebDriverWait class.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, it accepts two mandatory parameters: driver, and timeout; and two optional parameters: poll_frequency, and ignored_exceptions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;driver&lt;/strong&gt; — This is the instance of WebDriver you are using to perform your application testing. Example — Chrome, Remote, Firefox etc.,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;timeout&lt;/strong&gt; — It refers to the number of seconds before this wait fails and throws an exception.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;poll_frequency — polling frequency (optional parameter) is the wait/sleep time interval before WebDriverWait calls to check the conditions again. By default, it is 500 milliseconds in Selenium. You can modify the value as per your requirements. If you pass poll_frequency as “0”, the WebDriverWait &lt;strong&gt;init&lt;/strong&gt; constructor sets it back to 0.5, which is the default wait time between two callbacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ignored_exceptions&lt;/strong&gt; — WebDriverWait &lt;strong&gt;init&lt;/strong&gt; constructor is implemented in a way that it by default ignores NoSuchElementException. If your Selenium test automation script requires you to ignore more exceptions then pass a list of exceptions to ignored_exceptions attribute. The WebDriverWait constructor function extends its list of exceptions to be ignored by iterating on the list you pass.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Besides &lt;strong&gt;init&lt;/strong&gt;, WebDriverWait class also contains &lt;strong&gt;repr&lt;/strong&gt; object function which returns object representation in string format when repr() function is invoked on an object. In simpler terms, repr() function is used by other functions of WebDriverWait class to log useful information about the object on which it is invoked.&lt;/p&gt;

&lt;p&gt;The two important functions of WebDriverWait class that are used to introduce conditions are until and until_not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;until(self, method, message=''): This accepts a method as an argument and an optional message. until calls this method repetitively after a fixed time span (i.e. poll_frequency [500ms default]). The calling of the specified method stops only when the return value doesn’t evaluate to “False” i.e, till the method returns Success.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;until_not(self, method, message=''): until_not works much like until. The only difference is until_not repetitively calls for the method at a fixed time interval [poll_frequency] if it evaluates to True. Usually, it’s used when you want to wait until an element disappears.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WebDriverWait raises TimeoutException if the method doesn’t return True for until, or False for until_not.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "waitCreate")))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/expected-conditions-in-selenium-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Expected Conditions in Selenium&lt;/a&gt; is a heavily used convenience class with the WebDriverWait class of Selenium. The most common EC include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Alert_is_present&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Element_to_be_clickable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Element_to_be_selected&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Frame_to_be_available_and_switch_to_it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;New_window_is_opened&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Number_of_windows_to_be&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Presence_of_element_located&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Text_to_be_present_in_element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Title_contains&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Title_is&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Url_changes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Url_contains&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Url_matches&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implicit Waits in Selenium Python
&lt;/h2&gt;

&lt;p&gt;Implicit waits are implemented using &lt;strong&gt;implicitly_wait(time_to_wait)&lt;/strong&gt; function. This sets a sticky timeout per session (i.e. time to wait for executing a command or finding an element in a session). There is a good amount of difference between implicit wait and explicit wait in Selenium.&lt;/p&gt;

&lt;p&gt;Here, the WebDriver polls the DOM to find a WebElement for a specified duration before throwing an exception. The default time_to_wait argument value is set to “0”. Yes, that means it is disabled by default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example-&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.implicitly_wait(10)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Fluent Waits in Selenium Python
&lt;/h2&gt;

&lt;p&gt;Fluent waits are similar to Explicit Waits but they are still categorized as different wait types in the official Selenium documentation. Why has the docs listed them as two different types? For explicit waits, they avoided using non-mandatory function arguments like poll_frequency and ignored_exceptions (i.e, less specialized use, less control on the internal functionalities of WebDriverWait class).&lt;/p&gt;

&lt;p&gt;In the docs, they demonstrated using these 2 arguments under Fluent waits to gain more control over which exceptions should be ignored and how often should the driver poll the DOM. To put in black &amp;amp; white, fluent wait is a more articulate use of explicit wait.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example-&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebDriverWait(driver, 7, poll_frequency=5).until(EC.alert_is_present(), 'Timed out waiting for simple alert to appear')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Demonstration: Selenium Wait For Page To Load
&lt;/h2&gt;

&lt;p&gt;Now consider a simple example to demonstrate the usage of Selenium wait for ensuring the page to load. The below HTML script will be used for demonstration:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;    
    &amp;lt;body&amp;gt;

        &amp;lt;p&amp;gt;Click the button to make a BUTTON element with text.&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;The button element gets created after 3 seconds&amp;lt;/p&amp;gt;

        &amp;lt;button onclick="setTimeout(myFunction, 3000);"&amp;gt;Try it&amp;lt;/button&amp;gt;

        &amp;lt;script&amp;gt;
            function myFunction() {
                var btn = document.createElement("BUTTON");
                btn.innerHTML = "CLICK ME";
                btn.id = "waitCreate";
                document.body.appendChild(btn);
                setTimeout(function () { alert("I am created after 2 seconds of button waitCreate!"); }, 2000);

            }
        &amp;lt;/script&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When executed, the page will show as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbtps8dtzogrdta64cdy5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbtps8dtzogrdta64cdy5.png" width="511" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now when you will try to click on the button Try it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A “CLICK ME” button gets created after 3 seconds of clicking a pre-existing button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtyp8vfyt2xd07e33n08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtyp8vfyt2xd07e33n08.png" width="485" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An alert shows up after 2 seconds when the button is loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5wbxlt0xc1jti60ix8x2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5wbxlt0xc1jti60ix8x2.png" width="800" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was possible only with the help of Selenium wait. This caused the CLICK ME button to load, only after 3 seconds of when the Try it button is clicked, and the alert box to appear after its 2 seconds respectively.&lt;/p&gt;

&lt;p&gt;In the upcoming section, we shall show how to wait when interacting with these WebElements (i.e. button and alert box by using Python Selenium Wait). And we’ll also show the time taken by respective methods for polling the DOM and executing the commands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://www.lambdatest.com/blog/getting-started-with-cypress/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug04_kj&amp;amp;utm_term=kj&amp;amp;utm_content=" rel="noopener noreferrer"&gt;Cypress Testing&lt;/a&gt;: Get started with end-to-end automation testing using Cypress testing framework and LambdaTest test automation cloud.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Implement Explicit Wait and Fluent Wait in Selenium Using Python
&lt;/h2&gt;

&lt;p&gt;Consider the following implementation that uses the above webpage to realize the Selenium wait for page to load.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
import time
from datetime import datetime

class DragTest(unittest.TestCase):
    def setUp(self):
        # configuration to test in the cloud using lambdaTest
        username = "your user name"
        accessToken = "Your access token"
        gridUrl = "hub.lambdatest.com/wd/hub"
        desired_cap = {
            'platform' : "win10",
            'browserName' : "chrome",
            'version' :  "67.0",
            "resolution": "1024x768",
            "name": "LambdaTest python selenium wait testing automation",
            "build": "LambdaTest python selenium wait testing automation",
            "network": True,
            "video": True,
            "visual": True,
            "console": True,
        }
        url = "https://"+username+":"+accessToken+"@"+gridUrl 
        print("Initiating remote driver on platform: "+desired_cap["platform"]+" browser: "+desired_cap["browserName"]+" version: "+desired_cap["version"])
        self.driver = webdriver.Remote(
            desired_capabilities=desired_cap,
            command_executor= url
        )
        # self.driver = webdriver.Firefox()

    def test_selenium_wait(self):
        driver = self.driver
        driver.maximize_window()
        # printing time to demonstrate waits
        pageLoadClock = datetime.now()
        current_time = pageLoadClock.strftime("%H:%M:%S")
        print("Time before starting page load =", current_time)
        driver.get('https://pynishant.github.io/Selenium-python-waits.html')
        pageLoadedClock = datetime.now()
        current_time_after_page_loaded = pageLoadedClock.strftime("%H:%M:%S")
        print("Time after page load and before clicking the Try it button=", current_time_after_page_loaded)
        driver.find_element(By.XPATH, '//button[text()="Try it"]').click()
        # this is scripted to FAIL
        try:
            driver.find_element(By.XPATH, '//button[text()="CLICK ME"]').click()
        except Exception as e:
            ExceptionClock = datetime.now()
            current_time_Click_me_failed = ExceptionClock.strftime("%H:%M:%S")
            print("\nTime when click me was attempted to interact with but failed=", current_time_Click_me_failed)
            print("The below exception occured because we didn't wait for the element 'button' to be available before interaction.")
            print(e)

        # code to use explicit wait for CLICK ME button
        try:
            WebDriverClock = datetime.now()
            current_time_webdriver = WebDriverClock.strftime("%H:%M:%S")
            print("\nTime before waiting for CLICK ME button with webdriver=", current_time_webdriver)
            WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "waitCreate")))
            ButtonFound = datetime.now()
            current_time_ButtonFound = ButtonFound.strftime("%H:%M:%S")
            print("Time when Button Found=", current_time_ButtonFound)
            print("This was interacting with Button using Explicit wait. Now we shall interact with alert using fluent wait.")
        except Exception as e:
            print(e)
            print("error in try block")

        # Code to use FLUENT wait for ALERT's presence identification &amp;amp; handling
        try:
            alertClock = datetime.now()
            current_time_alertClock = alertClock.strftime("%H:%M:%S")
            print("\nTime before waiting for alert with webdriver=", current_time_alertClock)
            WebDriverWait(driver, 7, poll_frequency=5).until(EC.alert_is_present(), 'Timed out waiting for simple alert to appear')
            alertFound = datetime.now()
            current_time_alertFound = alertFound.strftime("%H:%M:%S")
            print("Time when Button Found=", current_time_alertFound)
            alert = driver.switch_to.alert
            time.sleep(1)
            alert.accept()
        except Exception as e:
            print(e)

    def tearDown(self):
        # closes the driver
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is the execution output when the Selenium test automation script is run on the cloud-based Selenium Grid by LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbte5vhx4vrqcwl96j6xb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbte5vhx4vrqcwl96j6xb.png" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Observe the time taken to find elements. “CLICK ME” button is polled using the default poll_frequency of WebDriverWait which is 500ms. Even though we gave a timeout argument of 10 seconds, the CLICK ME button’s presence was identified in exactly three seconds (i.e. the time after which it is created when we clicked the “Try it” button).&lt;/p&gt;

&lt;p&gt;But the presence of alert is polled using poll_frequency of 5 seconds. So, despite the fact that the alert was present merely after two seconds of CLICK ME button creation, it took Fluent WebDriverWait 5 seconds to identify its presence. This certainly clarifies the use of poll_frequency.&lt;/p&gt;

&lt;p&gt;A quick look at the execution video on the LambdaTest platform gives an indication that the waits are functioning as expected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2048%2F1%2AS4WTE8gwXvW6bbrxu_T-gg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2048%2F1%2AS4WTE8gwXvW6bbrxu_T-gg.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Implement Implicit Wait in Selenium Using Python
&lt;/h2&gt;

&lt;p&gt;Consider the below implementation that uses implicit Selenium wait for page to load. For brevity, we have only demonstrated waiting for the CLICK ME button using implicit wait in Selenium Python.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
import time
from datetime import datetime


class DragTest(unittest.TestCase):

    def setUp(self):
        # configuration to test in the cloud using lambdaTest
        username = "your user name"
        accessToken = "Your access token"
        gridUrl = "hub.lambdatest.com/wd/hub"
        desired_cap = {
            'platform': "win10",
            'browserName': "firefox",
            'version': "67.0",
            "resolution": "1024x768",
            "name": "LambdaTest python selenium wait testing automation",
            "build": "Implicit Wait",
            "network": True,
            "video": True,
            "visual": True,
            "console": True,
        }
        url = "https://" + username + ":" + accessToken + "@" + gridUrl
        print("Initiating remote driver on platform: " + desired_cap["platform"] + " browser: " + desired_cap[
            "browserName"] + " version: " + desired_cap["version"])
        self.driver = webdriver.Remote(
            desired_capabilities=desired_cap,
            command_executor=url
        )
        # self.driver = webdriver.Firefox()

    def test_selenium_wait(self):
        driver = self.driver
        driver.maximize_window()
        # defining condition for implicit waits - we have set 10 seconds
        driver.implicitly_wait(10)
        driver.get('https://pynishant.github.io/Selenium-python-waits.html')
        pageLoadedClock = datetime.now()
        current_time_after_page_loaded = pageLoadedClock.strftime("%H:%M:%S")
        print("Time after page load and before clicking the Try it button=", current_time_after_page_loaded)
        driver.find_element(By.XPATH, '//button[text()="Try it"]').click()
        # this won't FAIL with implicit time set
        try:
            # printing time to demonstrate waits
            pageLoadClock = datetime.now()
            current_time = pageLoadClock.strftime("%H:%M:%S")
            print("Time before starting polling for CLICK ME Button =", current_time)
            driver.find_element(By.XPATH, '//button[text()="CLICK ME"]').click()
            pageLoadClock = datetime.now()
            current_time = pageLoadClock.strftime("%H:%M:%S")
            print("Time after CLICK ME was found =", current_time)
        except Exception as e:
            print(e)

    def tearDown(self):
        # closes the driver
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Observe how the first try block succeeds and without introducing any explicit waits WebDriver waits for the CLICK ME button to be created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AIUvv2iAqyk8MSrdLjRuAlg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AIUvv2iAqyk8MSrdLjRuAlg.png" width="647" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2048%2F1%2AjLS-CqBy_WSa3iKwTLj8TA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2048%2F1%2AjLS-CqBy_WSa3iKwTLj8TA.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Types Of Selenium Waits in Python
&lt;/h2&gt;

&lt;p&gt;There are three more types of waits in Selenium Python :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Wait time for executing async JS scripts&lt;/strong&gt; — set_script_timeout(time_to_wait) is used to specify maximum wait time (in seconds) for execute_async_script() to complete execution of asynchronous JS scripts before throwing an error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Syntax: driver.set_script_timeout(30)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Wait time for page load time&lt;/strong&gt; — set_page_load_timeout(self, time_to_wait) is used to specify the maximum wait time (in seconds) for a page to load completely in a selenium WebDriver controlled browser. This is useful when you are performing Selenium automation testing in a throttling network condition.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Syntax: set_page_load_timeout(30)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sleep(time_to_sleep)&lt;/strong&gt; — This is a built-in Python function to halt the program for a specified number of seconds. However, the usage of sleep is not considered to be one of the &lt;a href="https://www.lambdatest.com/blog/selenium-best-practices-for-web-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug05_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;best practices for Selenium automation testing&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Syntax: Sleep(3000)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  polling2 Library for Selenium Wait in Python
&lt;/h2&gt;

&lt;p&gt;You can also use Python’s polling2 library to wait for elements in Selenium WebDriver. You will have to install polling2 library separately, using the below command:&lt;/p&gt;

&lt;p&gt;pip install polling2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example usage of polling2 library in Python&lt;/strong&gt;&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver

&lt;p&gt;driver = webdriver.Chrome()&lt;br&gt;
driver.get('&lt;a href="http://www.lambdatest.com'" rel="noopener noreferrer"&gt;http://www.lambdatest.com'&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;email_box = polling2.poll(lambda: driver.find_element_by_id(‘useremail’), step=0.5, timeout=7)&lt;br&gt;
email_box.send_keys('&lt;a href="mailto:email@email.com"&gt;email@email.com&lt;/a&gt;')&lt;br&gt;
sleep(2)&lt;br&gt;
driver.quit()&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AYzQ8X8866wAuHNmh21Jt2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AYzQ8X8866wAuHNmh21Jt2g.png" width="400" height="229"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog, we explored different waits of implementing Selenium wait for page to load in Python. Selenium waits will come handy when tests have to be run on WebElements that are loaded dynamically. Fluent wait in Selenium Python lets you control the polling frequency which is by default set to 250 ms in Explicit wait. Do let us know how you are using Selenium wait for page load in Python to tackle the dynamism of WebElements.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>selenium</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
  </channel>
</rss>
