Tuesday, 18 March 2014

Synchronization in selenium webdriver in python - Example

Synchronization makes selenium to wait for specific elements before performing operation on them. We can use below synchronization methods in selenium. 

1. Page Load Synchrnoization – Implicit Wait

We can set the default page navigation timeout. Below statement will set the navigation timeout as 50. This means that selenium script will wait for maximum 50 seconds for page to load.  If page does not load within 50 seconds, it will throw an exception.
driver.set_page_load_timeout(50)

2. Element Synchronization – Implicit Wait

We can set the default element existance timeout. Below statement will set the default object synchronization timeout as 20. This means that selenium script will wait for maximum 20 seconds for element to exist.  If Web element does not exist within 20 seconds, it will throw an exception.
driver.implicitly_wait(20)

3. Synchronization based upon specific condition – Explicit Waits

We can also insert custom synchronization points in the script using WebDriverWait class. Please remember that you have to import this class before you use it.
from selenium.webdriver.support import expected_conditions as EC

We can also instruct selenium to wait until element is in expected condition.
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)

element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))


from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

# available since 2.4.0

from selenium.webdriver.support import expected_conditions as EC

# available since 2.26.0

ff = webdriver.Firefox()

ff.get("http://somedomain/url_that_delays_loading")

try:

    element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))

finally:

    ff.quit()





What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

About Me

My photo

Self-motivated software professional