How To: Fluent Wait In Selenium (2 Min) Using Python & Pytest

preview_player
Показать описание
In this tutorial, you'll learn how to use fluent wait in Selenium using Python and Pytest.



Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to use weight in selenium using Python. Let's start by looking at the scenario.

I want to go to this specific URL and then wait for the bottom bar to become visible. Once the bar is visible I want to assert whether the keyword land appears in the paragraph text or not. Now, let's look at the test directory.

On line 14, I'm using the webdriverweight class to wait for a maximum of 15 seconds until the bottom bar is visible. I'm also specifying a pole frequency of 2 seconds and I have of ignored the exceptions defined this is called fluent weight in selenium and it's a type of explicit weight. It's considered an intelligent type of weight because you use it with expected conditions and it's confined to only one web element.

On line 18, I'm using the get underscore attribute method to get the inner HTML text of the paragraph tag. Finally, on 21, I'm asserting whether the keyword land appears in the text or not. Now let's run this program to see what the execution looks like.

As you can see our test passed. Watch what happens when I increase the pole frequency to 20 seconds which is greater than our timeout. Now when I rerun the program you'll notice that our test will fail and if you look at the logs you'll see that it failed because of a timeout exception.

Note, it's not a recommendation to mix implicit and explicit weight because it can cause unpredictable wait times. There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.



def test_wait(driver):

# Fluent wait: will wait for a max of n sec until a specific element is located (type of explicit wait)
# Intelligent wait used with expected_conditions confined to one web element
# Can specify poll frequency and list of ignored exceptions
# Don't mix implicit & explicit/fluent waits, it can cause unpredictable wait times
WebDriverWait(driver, timeout=15, poll_frequency=20,
ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException])\

print("text:", text)
assert 'land' in text
Рекомендации по теме