[Python] Python Selenium always return first result

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to read all the details about the ads on page


    When reading the element "//div[@class='seznam']" I get basic information about all the ads. But I would like to read all the elements "" or the element "", but I always get data of the first ad on the page. I tried with scrolling on page , I also use the function "find_elements" instead of "find_element".

    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver import ActionChains, Keys
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.support import expected_conditions as EC
    import time
    from seleniumbase import Driver
    from selenium.webdriver.remote.webelement import WebElement

    driver = Driver(uc=True)
    wait = WebDriverWait(driver, 60)
    URL = "https://www.nepremicnine.net/oglasi-prodaja/gorenjska/kranj/kranj/stanovanje/letnik-od-1980-do-1989/"
    driver.open(URL)

    wait.until(EC.element_to_be_clickable((By.ID,"CybotCookiebotDialogBodyButtonDecline"))).click()
    cnt_oglasov = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='oglasi_cnt']")))
    print(cnt_oglasov.text)

    if (cnt_oglasov.text.find('Št. ustreznih oglasov: '))>=0:
    st_oglasov_str=cnt_oglasov.text.removeprefix("Št. ustreznih oglasov: ")
    print("st. najdenih oglasov: " + st_oglasov_str)

    #list of ads
    driver.execute_script("window.scrollTo(0, 1000);")
    time.sleep(5)
    vsebina = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='seznam']")))
    #print(vsebina.text)
    #ActionChains(driver).move_to_element(vsebina)
    posamezni = vsebina.find_elements(By.CLASS_NAME, 'property-details')
    title = []
    for one in posamezni:
    title = one.find_element(By.XPATH, "//div[@class='propertydetails']")
    title1 = title.get_attribute('data-href')
    print(title1)

    time.sleep(5)

    Continue reading...

Compartilhe esta Página