Selenium Python只给出一个结果

drnojrws  于 2022-12-13  发布在  Python
关注(0)|答案(1)|浏览(138)

Selenium只返回了我的第一个结果。
我的代码:

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)

我只想提取div中的每个汽车名称和信息
尝试在前面放置.//点,但没有任何内容。尝试使用元素而不是元素,但没有任何内容。尝试使用完整xpath和普通xpath,但没有任何内容。
当我打印(n)时,它只给我所有汽车的所有信息,但我只需要一些信息,这可能吗?

vlju58qv

vlju58qv1#

要获取汽车名称列表,请显式使用wait WebDriverWait()和wait for visibility_of_all_elements_located()以及后面的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)

您需要导入以下库

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

控制台输出:

['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']

相关问题