How To: Implicit Vs. Explicit Wait In Selenium (2 Min) Using Python

preview_player
Показать описание
In this tutorial, you'll learn difference between implicit wait vs. explicit wait in Selenium.



Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn the difference between implicit and explicit weight in Selenium. Let's start by looking at the test scenario. I want to go to this specific URL and wait for the bottom bar to be visible.

On line 11, I'm specifying an implicit weight of 5 seconds. This means that the web driver will wait for a maximum of 5 seconds to check the presence of a web element. Once you specify an implicit weight, it's available for the entire life of the web driver instance and it applies to all web elements.

On line 15, I'm specifying an explicit weight of 20 seconds. Here I'm asking the web driver to wait for a maximum of 20 seconds until the bottom bar is visible. Explicit weight is considered more intelligent than implicit weight because you use it in conjunction with expected conditions.

Note, unlike implicit weight, explicit is confined to just one web element. On line 18, I'm using the get underscore attribute method to get the inner HTML text of the second button in the bottom bar. Finally, on line 21, I'm asserting whether the text is equal equal to one dollar downland or not.

Not let's run this program to see what the execution looks like. As you can see our test passed as expected. Watch what happens if I reduce the explicit weight to 5 seconds and rerun the program.

This time our test failed because the bottom bar was not visible within 5 seconds. This can be confirmed by the timeout exception in the console logs. Note theoretically we could have used a bigger implicit weight instead of using an explicit weight nut that's not recommended because it slows down the execution when you're running a large number of tasks in batch.

There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.



def test_wait(driver):

# Implicit wait: will wait for a max of n sec for all web elements
# Available for the entire life of the web driver instance

# Explicit wait: will wait for a max of n sec until a specific element is located before proceeding further
# Intelligent wait used with expected_conditions confined to one web element

print("text:", text)
assert text == 'Buy $1 Down Land'

import pytest
from selenium import webdriver
import os

def driver():
driver = webdriver.Chrome(executable_path=root_dir + '/resources/chromedriver')
# Time in seconds driver should wait when searching for an element if it is not
# immediately present
yield driver
Рекомендации по теме