1

I'm trying to do a wait in which the driver waits until all the elements of the same class are located.

For Example:

If class is foo

I try:

WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'foo')))

I think this only waits for the first occurring element in that class. Anyone know how I can wait until all elements of that class are located.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
AaravM4
  • 380
  • 1
  • 4
  • 21

1 Answers1

1

WebDriverWait inconjunction with the expected_conditions as presence_of_element_located() will wait for the very first matched WebElement.

To wait until all the elements of the same class e.g. foo class are present, instead of presence_of_element_located() you need to induce WebDriverWait for the presence_of_all_elements_located() and your effective code block will be:

WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'foo')))

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    How will Selenium know "all" elements are located before all elements are located? The comment of this method is `An expectation for checking that there is **at least one** element present on a web page` – nilknow Dec 27 '20 at 07:59
  • 1
    @poorguy I think you're right. This answer is misleading and so is the Selenium documentation. Looking at the Selenium source code, I think there is no difference between the two methods. The first is satisfied if `findElement` (the first instance) returns something, and the second is satisfied if `findElements` (a list of all instances found so far) returns something. This makes them effectively equivalent and redundant in functionality. It also means the answer above is incorrect and, unless Selenium can predict the future, there is in fact no way for it to know that all elements were found. – Steven Aug 15 '21 at 16:50