[Python] Loop through web elements using Selenium wait strategy

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to perform what I thought was a fairly simple task. For each element in a list of web elements, I want to find a href, go to the page, get data, and move on to the next element. Here is my function.

    def get_ads_dates(driver, base_url):
    objects_dates = []
    wait = WebDriverWait(driver, 5)
    driver.get(base_url)
    # ads - is a list of web elements
    ads = driver.find_elements(By.CLASS_NAME, 'object-cards-block.d-flex.cursor-pointer')
    for ad in ads:
    try:
    ad_link = wait(ad, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.main-container-margins.width-100 > a')))
    except NoSuchElementException:
    print('no such element')
    # following the link
    driver.get(ad_link.get_attribute('href'))
    ad_date = driver.find_element(By.XPATH, '//*[@id="Data"]/div/div/div[4]/div[2]/span').text
    objects_dates.append(ad_date)


    The possibility of using the wait().until(EC.presence_of_element_located()) construction I've found here and here. But I am getting TypeError: 'WebDriverWait' object is not callable.

    • If I am not using wait(ad,5) - I'm getting StaleElementReferenceException.
    • If I am using wait without attribute ad (that is, not indicating directly elements in the list) - I'm getting data only for the first elemenmt. How can I loop through all web-elements in the list using wait strategy?

    Python 3.11. PyCharm

    Continue reading...

Compartilhe esta Página