In this tutorial, you'll learn difference between implicit wait vs. explicit wait in Selenium.
—
Facebook: / gokcedbsql
—
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.
Once it's visible, I want to check whether the text in the second button is equal to one dollar downland or not. Now let's look at the test directory. I haven't had a conf test.py file that contains the driver fixture function.
This function is responsible for initializing the Chrome web driver. Next, let's look at our test function. On line 7, I'm using the driver.get method to go to the specific URL.
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.
Connect MySQL In Python: • How To Connect To MySQL In Python (2 ...
Run Selenium PyTests In Parallel: • How To Run Selenium PyTests In Parall...
Find_elements() in Selenium: • How To: Find_Elements() In Selenium (...
—
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
def test_wait(driver):
driver.get("https://gokcecapital.com/free-land")
Implicit wait: will wait for a max of n sec for all web elements
Available for the entire life of the web driver instance
driver.implicitly_wait(20)
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
WebDriverWait(driver, 5).until(ec.visibility_of_element_located((
By.XPATH, '//*[@id="bottom-button-p"]/a[2]/span')))
text = driver.find_element(By.XPATH, '//*[@id="bottom-button-p"]/a[2]/span').get_attribute('innerHTML')
print("text:", text)
assert text == 'Buy $1 Down Land'
import pytest
from selenium import webdriver
import os
@pytest.fixture()
def driver():
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
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
driver.implicitly_wait(2)
yield driver
driver.quit()
Смотрите видео How To: Implicit Vs. Explicit Wait In Selenium (2 Min) Using Python онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Gokce DB 21 Апрель 2022. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 151 раз и оно понравилось 6 посетителям.