Python : selenium 查找元素 angular

Python : selenium find element angular

printscreen

我试图阅读这一行,但它不起作用:

element = driver.find_element(By.XPATH,"//span[contains(@_ngcontent-whw-c159 data-cy,'hotel-title')]")
print(element.text)

有人知道怎么做吗?

hotel_names=[x.text for x in driver.find_elements(By.XPATH,"//h1[@class='title']")]

要获取该页面上的所有名称,您可以获取标题为 class 的所有 h1。

wait=WebDriverWait(driver,10)                                 

driver.get("https://booking.luxairtours.lu/?lang=fr&adults=2&children=0&childages=0&sortBy=TotalPrice&sortOrder=desc&departuredate=2022-07-01&returndate=2022-07-17&themes=Custom6&durationstart=5&durationend=8")
hotel_names=[x.text for x in wait.until(EC.presence_of_all_elements_located((By.XPATH,"//h1[@class='title']")))]
print(hotel_names)

为确保加载使用 webdriver 等待元素出现。

进口:

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

输出:

['LUXiClub Atlantica Aegean Blue Resort', 'LUXiClub Grecotel Marine Palace & Aqua Park', 'LUXiClub Insotel Punta Prima Resort & Spa', 'LUXiClub Sol Nessebar Palace', 'LUXiClub Grifid Club Hotel Bolero', 'LUXiClub Paloma Grida', 'LUXiClub Akassia Swiss Resort', 'LUXiClub Sol Nessebar Resort', 'LUXiClub Grand Hotel Holiday Resort', 'LUXiClub Relaxia Lanzasur Club']