Home > Software engineering >  Selenium Python just gives one result
Selenium Python just gives one result

Time:12-12

Selenium is only returning me first result.

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(15)# 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')

for n in dealers:
    name = n.find_element(By.TAG_NAME,"/html/body/div[1]/div[4]/div/div[3]/div[1]/div/div[1]/div[1]/h3/a")
    print(name.text)

I just want to extract each car name and the info that is inside the div

Tried putting an .// a dot before and nothing. Tried elements instead of element and nothing. tried full xpath normal xpath and nothing.

It only gives me all info for all cars when I print(n) but I need only some info, is that possible?

CodePudding user response:

To get the list of car names use explicit wait WebDriverWait() and wait for visibility_of_all_elements_located() and following css selector.

driver.get('https://www.tajeran-group.de/fahrzeuge/')
carnamnes=[carname.text for carname in WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".b2b-vehicle-list .uk-card-header h3 a")))]
print(carnamnes)

You need to import below libraries

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

Console output:

['Audi A1 S-line MMI-Navi PDC Klimaauto. Spurassist.', 'Audi A3 design ACC Bi-Xenon Navi virtual Cockpit AHK', 'Audi A3 S-line sport ACC LED Navi PDC SHZ Virtual', 'Audi A3 Sport AHK Leder PDC Bi-Xenon', 'Audi A3 sport S-line ACC Leder MMI Navi PDC SHZ DAB', 'Audi A3 Sportback voll LED CarPlay SHZ PDC Start/Stop', 'Audi A4 2.0 TFSI Ultra Hybrid Sport Bi-Xenon Navi PDC', 'Audi A4 35 TFSI Hybrid Xenon PDC Connectivity-Paket MMI PDC', 'Audi A4 Audi Glanz-Paket Navi AHK Xenon-Plus PDC-Plus', 'Audi A4 Avant 35 TDI S-line Navi PDC SHZ Teilleder', 'Audi A4 g-tron S-Line sport Navi Matrix Leder PDC SHZ', 'Audi A4 g-tron S-Line sport Navi Matrix PDC SHZ Leder', 'Audi A4 MMI-Navi Bi-Xenon Leder Tempomat Euro6 I-Hand', 'Audi A4 Navi Standheiz. Bi-Xenon PDC SHZ', 'Audi A4 S-Line BiXenon ACC TeilLeder Navi 18° BT I.Hand', 'Audi A4 S-Line Dachbox TV ACC Pano. AHK TeilLeder Cam', 'Audi A4 S-Line Navi Bi-Xenon PDC SHZ Teilleder', 'Audi A4 Sport ACC S-tronic LED Sound DAB I.Hand', 'Audi A4 Ultra Leder virtual cockpit LED AHK RCAM SOUND', 'Audi A4 Ultra Leder virtual cockpit LED AHK RCAM SOUND']
  • Related