It may not really answer question, I just want to save my custom wait function here :D
I don't like WebDriverWait()
it need another import that not really easy to remember or type and it also used another function to select element.
Example:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME , 'myClass')))
here custom wait to use with default function
import time
def waitFor(maxSecond, runFunction, param):
while maxSecond:
try:
return runFunction(param)
except:
time.sleep(0.5)
maxSecond -= 0.5
Usage:
# wait 5 second
waitFor(5, driver.find_element_by_class_name, 'myClass')
# Or
link = waitFor(5, driver.find_element_by_class_name, 'myClass')
link.click()
# Or
link = waitFor(5,....)
if not link:
print('Timeout, link not found')
and for reference
To find single elements
- find_element_by_id
- find_element_by_name
- find_element_by_xpath
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_tag_name
- find_element_by_class_name
- find_element_by_css_selector
To find multiple elements (these methods will return a list):
- find_elements_by_name
- find_elements_by_xpath
- find_elements_by_link_text
- find_elements_by_partial_link_text
- find_elements_by_tag_name
- find_elements_by_class_name
- find_elements_by_css_selector