pythonselenium-webdriverappiumpython-appium

Waiting for an element to appear in Selenium (WebDriverWait)


When we receive a list of elements in Selenium, with the "find_elements" command, we select an element from this list, for example element number 2 in the list

Example: driver.find_elements(By.CLASS_NAME, 'container bg-black-100')[2]

Now my question is if I want to write a command that waits for your element 2 to appear in the list which is my target how should I set up the following code!!

WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CLASS_NAME, 'container bg-black-100')))

Solution

  • The locator expression you're looking for would be:

    *WRONG

    WebDriverWait(driver, 60).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'container bg-black-100')))[2]
    

    *WRONG

    Edit: The above locator is incorrect, as CLASS_NAME locators only work with single classnames. For multiple classnames and spaces, you can use XPATH:

    WebDriverWait(driver, 60).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@class="container bg-black-100"]')))[2]
    

    I'm leaving the wrong example here, to exemplify how easy it is to get mixed up between different Selenium locators.

    Selenium has a reasonably decent documentation here.