Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for number of elements to be loaded using Selenium and Python

Let's say I'm selecting with the selector:

//img[@data-blabla]

And I want to wait for 10 elements to be loaded, not just one.

How would this be modified? I'm making a guess with the index [9]

WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.XPATH, '//img[@data-blabla][9]')))
like image 304
User Avatar asked Oct 25 '25 17:10

User


1 Answers

To wait for 10 elements to load you can use the lambda function and you can use either of the following Locator Strategies:

  • Using >:

    myLength = 9
    WebDriverWait(browser, 20).until(lambda browser: len(browser.find_elements_by_xpath("//img[@data-blabla]")) > int(myLength))
    
  • Using ==:

    myLength = 10
    WebDriverWait(browser, 20).until(lambda browser: len(browser.find_elements_by_xpath("//img[@data-blabla]")) == int(myLength))
    
like image 136
undetected Selenium Avatar answered Oct 28 '25 08:10

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!