- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the various waits available in Selenium with python?
While working with Selenium, there may be situations when we see that after page load action by the browser, the web elements are getting loaded at various intervals of time.
This type of situation leads to syncing problems between Selenium and the web element on the page. The identification of elements does not happen due to the absence of that element in DOM. The exception like ElementNotVisibleException is thrown due to this.
The wait concept in Selenium overcomes this problem and gives a delay between elements identification and actions performed on them. Selenium web driver supports mainly two types of waits −
This is a global wait applied to all the elements on the page. The wait time is provided as arguments to the method. For example, if the wait time is 5 seconds, it shall wait this period of time before throwing a timeout exception.
An implicit wait is a dynamic wait which means if the element is available at the third second, then we shall move to the next step of the test case instead of waiting for the entire five seconds.
An implicit wait informs the web driver to poll for a specific amount of time. Once this time is determined, it remains for the entire driver session. The default time of an implicit wait is 0.
The explicit wait is applied not to all but to a specific element on the page. It is actually a condition which has to be met before moving to the next step of the test case.
The explicit wait is also a dynamic in nature which means the wait will be there as long as necessary. Thus if the wait time is five seconds and our given condition is satisfied at the third second, we need not halt the execution for the next two seconds.
The webdriverWait class along with expected_conditions is used to create an explicit wait. The webdriverWait class can call the ExpectedCondition after every 500ms by default for checking if the condition is met.
w = WebDriverWait(driver, 6) w.until(expected_conditions.presence_of_element_located((By.CLASS_NA ME, "Tutorialspoint")))
The expected conditions commonly used in explicit wait are listed below −
Code Implementation with implicit wait.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") #setting implicit wait 10 seconds driver.implicitly_wait(10) # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() # identifying the edit box with the help of id driver.find_element_by_id("gsc-i-id1").send_keys("Selenium") #to close the browser driver.close()
Code Implementation with explicit wait.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") #setting implicit wait 10 seconds driver.implicitly_wait(10) # to maximize the browser window driver.maximize_window() driver.get("https://www.tutorialspoint.com/tutor_connect/index.php") #to refresh the browser driver.refresh() # identifying the link with the help of link text locator driver.find_element_by_link_text("Search Tutors").click() w.(expected_conditions.presence_of_element_located((By. CSS_SELECTOR, "input#txtFilterLocation"))) #to close the browser driver.close()
- What are the various methods available under Select class in Selenium?
- What are the differences between implicit and explicit waits in Selenium with python?
- What are the various Components of Selenium?
- What are the various important exceptions in Selenium?
- What are the various JSON files available in C# ASP.NET Core?
- What are the various locators that Selenium supports?
- What are assertions available to test relational comparisons in Selenium with python?
- What are the different types of wait available in Selenium?
- What are the methods available for handling static dropdowns in a page in Selenium with python?
- Name the various annotations available in TestNG.
- Describe some of the exceptions available in Selenium with python?
- Is navigate method available in Selenium Webdriver with Python?
- What are assertions in Selenium with python?
- What are the various Smartphone Sensors?
- What are ActionChains class in Selenium with python?