Explicit Wait in Selenium

Explicit Wait in Selenium

In this article, we would like to demo how to wait specific web element to be available before we can operate it.

For example, we may wait for the search field or button to be visible before we can input search text.

In selenium, wait for specific web element available is called “Explicit Wait”

 

Testing Scenario

In this sample script, we are going to

1. visit the web page, http://www.seleniumhq.org/.

2. Wait until the search field available

3. input “Selenium” once the search field is ready.

 

Source code sample

 

[pastacode lang=”python” message=”Explicit Wait” highlight=”15,3,4″ provider=”manual”]

# This is sample code of Explicit wait. By Tony Hsu
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
import unittest
class ExplicitWaitTests(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://www.seleniumhq.org/")

    def test_SeleniumSearch(self):

        SearchText = WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, "//input[@id='q']")))
        SearchText.send_keys("selenium")


    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main(verbosity=2)

[/pastacode]

Leave a Reply

Your email address will not be published. Required fields are marked *