无法从标签中提取 href

Unable to extract hrefs from a tags

我正在尝试从网页中提取两辆汽车(标记为红色)的 href。这些 hrefs 在标签下,为了访问它们,我尝试使用 CSS_selectors 和 XPath 但没有成功。提前谢谢你。

driver.get("https://www.akrapovic.com/en/car/products/Ferrari?brandId=20")
    outer_tag = driver.find_element(By.XPATH,'//*[@id="ak-app"]/div[1]/abstract/products/section/product-listing/div/div/div[3]/div[1]/div[2]')
    print(outer_tag.find_element(By.XPATH,'//a').get_attribute('href')

不确定您想要的特定 href 元素,但这会获取您突出显示的项目的标签的 href

driver.get("https://www.akrapovic.com/en/car/products/Ferrari?brandId=20")
outer_tag = driver.find_elements_by_class_name('item-wrapper')
print(outer_tag[0].get_attribute('href'))

获得

https://www.akrapovic.com/en/car/products/Ferrari/458-Italia-458-Spider?brandId=20&modelId=70
wait=WebDriverWait(driver,10)
driver.get('https://www.akrapovic.com/en/car/products/Ferrari?brandId=20')
hrefs=[x.get_attribute('href')for x in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"a.animated.item-wrapper.model")))]
print(hrefs)

要等待元素的可见性,请使用 webdriver 等待,然后使用 get_attribute.

获取元素的 href

输出:

['https://www.akrapovic.com/en/car/products/Ferrari/458-Italia-458-Spider?brandId=20&modelId=70', 'https://www.akrapovic.com/en/car/products/Ferrari/488-GTB-488-Spider?brandId=20&modelId=785']

导入:

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