Home > Software engineering >  Selenium only prints first result and I don't know why
Selenium only prints first result and I don't know why

Time:12-12

My code :

from selenium.webdriver.common.by import By
from selenium import webdriver
import pandas as pd

url = 'https://www.tajeran-group.de/fahrzeuge/'

PATH = 'C:\\Users\\czoca\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.6\\chromedriver.exe'

driver = webdriver.Chrome(PATH)
driver.get(url)



driver.maximize_window()# For maximizing window
driver.implicitly_wait(10)# gives an implicit wait for 20 seconds

dealers = driver.find_elements(By.XPATH, '/html/body/div[1]/div[4]/div/div[3]/div[1]/div/div[1]')

for n in dealers:
    name = n.find_element(By.XPATH, "/html/body/div[1]/div[4]/div/div[3]/div[1]/div/div[1]/div[1]/h3/a")
    km = n.find_element(By.XPATH, "/html/body/div[1]/div[4]/div/div[3]/div[1]/div/div[1]/div[2]/div/div[2]/div/div[1]/ul/li[1]/span")
    firstreg = n.find_element(By.XPATH,"/html/body/div[1]/div[4]/div/div[3]/div[1]/div/div[1]/div[2]/div/div[2]/div/div[1]/ul/li[2]/span")

    print(name.text,km.text,firstreg.text)
    #print(email.text)

I tried adding s to "element" and did not work, tried just print(n.text) only gives me 1 result.. The website is : https://www.tajeran-group.de/fahrzeuge/ i want to get all info for each car that is just it.. any ideias? Thanks

CodePudding user response:

Try with something like this:

dealers = driver.find_elements(By.XPATH, //div[@class='uk-card uk-card-small uk-box-shadow-small uk-card-default uk-margin-bottom']//div[@class='uk-card-header'])

or at least to match all elements not only the first one:

/html/body/div[1]/div[4]/div/div[3]/div[1]/div/div[*]
  • Related