8

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()

2
  • In the examples, it should be EC.presence_of_element_located((By.XPATH, 'xpath')), not ByXPATH(('xpath)) Commented Mar 1, 2017 at 14:25
  • 1
    Please correct me someone if I am wrong, but the above implementation is ExplicitWait and not ImplicitWait right? Because according to documentation we call implicit wait as driver.implicitly_wait(10) right? Commented Mar 30, 2021 at 6:26

2 Answers 2

24

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you describe what language you used, cz in my python wait.until(EC.presence_of_all_elements_located((By.XPATH, "//table[@id='someTable']/tbody/tr/td[7]"))) make my container request another container to run(i mean error).
7

The problem here is that it is simpler for a single item. It just has to wait until your locator returns a single, or more, elements.

When you deal with multiple elements, WebDriver cannot possibly know how long to wait because it has no idea about how many elements you expect to be there.

So you'll have to use an explicit wait instead.

In this explicit wait, you should:

  1. Run find_elements_by_path
  2. Check the result from the step is a collection that contains the amount of elements you need. If this isn't equal to the number you expect, you can let the "waiter" fail fast and go round again.
  3. If the above is true, you can exit your "waiting", otherwise, let the "waiter" go round again.

http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.