Home > Software design >  What does WebDriverWait(driver, 20) means?
What does WebDriverWait(driver, 20) means?

Time:04-23

I am working with the following selenium code:

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

PATH= r"C:\Users\Hamid\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.google.com/")
click_button=driver.find_element_by_xpath('//*[@id="L2AGLb"]/div').click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")
search=driver.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]').click()

I am not sure however what the breakdown for the following line of code is:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")

What does webdriverwait mean? what does 20 refer to? what is meant by EC element? why does one need to wait? what does q refer to?. If I wanted to use this same code to work on a different item on the page what would I typically change?

CodePudding user response:

As per the API documentation of WebDriverWait the constructor takes a WebDriver instance and timeout in seconds as arguments.

class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

where,
    driver: Instance of WebDriver (Ie, Firefox, Chrome or Remote)
    timeout: Number of seconds before timing out
    poll_frequency: sleep interval between calls By default, it is 0.5 second.
    ignored_exceptions: iterable structure of exception classes ignored during calls. By default, it contains NoSuchElementException only.
    

As an example, using the mandatory arguments:

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))

Using lambda expression:

element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId")) 

Using all the arguments:

is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
  • Related