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

徘徊边缘 提交于 2020-12-13 03:23:05

问题


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]')))

回答1:


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


来源:https://stackoverflow.com/questions/64746509/how-to-wait-for-number-of-elements-to-be-loaded-using-selenium-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!