Jasmitha Meka
Jasmitha Meka

Reputation: 1597

Difference between find_elements_by_<attribute> and WebDriverWait(self.driver, 20).until(EC.presence_of_element_located(locator)

I have recently started working on selenium-python. Both of these give same output. I want to know if there is any difference

time.sleep(10)
element = self.driver.find_element_by_xpath(<some-xpath>).get_attribute('textContent')

and

element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, <some-xpath>))).get_attribute('textContent')

Upvotes: 0

Views: 181

Answers (1)

supputuri
supputuri

Reputation: 14135

With your first code

time.sleep(10)
element = self.driver.find_element_by_xpath(<some-xpath>).get_attribute('textContent')

your script will wait for 10 seconds and then return the element that's matching with the xpath. However, when you use the second code

element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, <some-xpath>))).get_attribute('textContent')

the element will be returned as and when it's available. Script will keep checking for the element at a max of 10 seconds.

So, always better to use the ExplicitWait as shown in the second part of your code.

Consider that the element is displayed with in 3 seconds, then you are wasting 7 seconds in the first approach, but with second approach the script will move-on to the next step right after the element is present on the 3rd second (saving 7 seconds of execution time for one element, think about this in the larger scale).

Upvotes: 2

Related Questions