Home > database >  Selenium does not find all the matching elements, and returns a different number of elements each ti
Selenium does not find all the matching elements, and returns a different number of elements each ti

Time:02-06

I'm trying to find elements. Everything I tell it to find, it all returns an empty list. Trying every way - xpath, tag_name, class. The generalization returns empty lists, or each time a different number of elements.

enter image description here

I need the href of all the apartments.

first I tried the simple code (that worked all the time):

    driver = webdriver.Chrome(
        executable_path="C:\\Users\\user\\Documents\\לימודים\\python\\selenium-   project\\Driver\\chromedriver.exe")
    driver.get("https://www.ad.co.il/nadlansale?sp3=30&sp1=1,82,41,103,28,101,110")
    driver.maximize_window()
    time.sleep(30)
    links_list = driver.find_elements_by_xpath('//*[@id="cards"]/div/div/div[2]/a')
    print(len(links_list))

I also tried by class_name, and nothing, I also tried this code:

    driver = webdriver.Chrome(
        executable_path="C:\\Users\\user\\Documents\\לימודים\\python\\selenium-   project\\Driver\\chromedriver.exe")
    driver.get("https://www.ad.co.il/nadlansale?sp3=30&sp1=1,82,41,103,28,101,110")
    driver.maximize_window()
    time.sleep(30)
    try:
        links_list = WebDriverWait(driver, 10).until(
            EC.presence_of_all_elements_located((By.XPATH, '//*[@id="cards"]/div/div/div[2]/a'))
        )
        print(len(links_list))
    finally:
        driver.quit()

CodePudding user response:

With this xpath you should get all href

//*[@id='cards']//a

enter image description here

CodePudding user response:

You could do it like this:

links_list = driver.find_elements(By.XPATH,"//div[contains(@class, 'card-body p-md-3')]/a")
href_links = [link.get_attribute("href") for link in links_list]
print(href_links)
  • Related