-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Description
AJAX-related problems with find_element()
I have some Python code (which my employer has authorized me to release under the Apache 2.0 license). By modifying webdriver.py, it is possible to change the behavior of find_element() and related methods when additional parameters are present. The goal is to selectively invoke WebDriverWait() instead of self.execute(Command.FIND_ELEMENT) when the parameters include an optional timeout parameter with a value greater than the default value of 0.
Why would anyone want to do this? When using a script recorder like Selenium Builder with an AJAX web application, there will be a failed call to find_element() whenever the script is looking for an element that has not loaded yet.
Example:
wd = webdriver.Firefox()
wd.find_element_by_id("my-element-id").click()
Fails when the element is not yet in the DOM because it loads dynamically in response to user input
Workaround #1:
element = WebDriverWait(wd, timeout ).until( expected_conditions.element_to_be_clickable( ( By.ID, “my-element-id” ) ) )
element.click()
Workaround #2:
errCount = 0
while errCount < 5:
try:
wd.find_element_by_id("my-element-id").click()
break
except:
time.sleep(5)
errCount += 1
continue
Proposed Solution
Neither of the above-mentioned workarounds is desirable because of the number of times it has to be coded into the script for a typical AJAX web application. Instead, it would be better to find a quick way to fix broken find_element() calls.
Example:
wd = webdriver.Firefox()
wd.find_element_by_id("my-element-id").click()
Breaks if we look for the element before the server renders it
wd.find_element_by_id("my-element-id",10,”clickable”).click()
Succeeds because of modifications to webdriver.py; using WebDriverWait with a 10 second time limit for the element to become clickable.
I have code that implements the concept; will attempt to demonstrate in a branch.