Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Selenium - implicit wait for multiple elements

Currently I use implicit wait to locate elements before issuing any action upon them. See example of implicit wait:

WebDriverWait(browser,10).until(EC.presence_of_element_located(By.XPATH(('xpath')))

This works fine when dealing with a single element. However, it appears that if the xpath relates to multiple elements then EC.presence_of_element_located() will time out. My question is, how do I do an implicit wait for multiple elements?

Clarification:

Single element -

WebDriverWait(browser,10).until(EC.presence_of_element_located(By.XPATH(('xpath')))
browser.find_element_by_xpath('xpath')

Multiple element -

??
browser.find_elements_by_xpath('xpath')

Note: Notice use of find_elements_by_xpath() in multiple element instance instead of using find_element_by_xpath()

like image 922
Phoenix Avatar asked Mar 28 '14 10:03

Phoenix


1 Answers

I'm over two years late but I want to post this in case someone googles their way here like I did. You can use

WebDriverWait(browser, 10).until(
    EC.presence_of_all_elements_located((By.XPATH, 'xpath'))
)

and that will return all of them. You don't need to do

browser.find_element_by_xpath('xpath')

after your explicit wait because WebDriverWait(browser,10).until(...) will return the element(s) you are waiting for.

like image 199
Imad Sawaya Avatar answered Sep 18 '22 18:09

Imad Sawaya