pikrut
0
Q:

waits in selenium

How many types of waits do you know in Selenium?
  There are 3 types of wait I use
    1- =Implicit Wait=
  - Implicit wait is triggered when driver is not able to find web element.
  - Driver will wait for the given time or until it finds the element.
  - If driver does not find web element, it will throw NoSuchElementException.

   2- =Explicit Wait=
- I use it when I want to wait for specific conditions or 
  specific web elements. 
    - Such as: elementIsClickable
    - elementIsDisplayed
    - titleIs...
       - To be able to use explicit wait:
       - You need to create instance of WebDriverWait class.
      WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a/h3")));
  
3- Thread.sleep
	I avoid to use it since it makes my test heavier and slower
   
1
//required import
WebDriver driver = new ChromeDriver();
//											  Timeout in seconds
WebDriverWait wait = new WebDriverWait(driver, 15);

//Three most common explicit waits

//waits until the element is visible and can be clicked
wait.until(ExpectedConditions.elementToBeClickable(By.id("button1")));

//waits until the element is visible on the page somewhere i.e. pixels > 0
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("text_box1")));

//waits until the element is removed from the page
//This one is helpful when moving from one page to another
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("text_box1")));

//implicit wait will wait a set time, similar to Thread.sleep();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//There is also a FluentWait, but I am unfamiliar with it 
2
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 as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()
0

New to Communities?

Join the community