Home > Blockchain >  Unable to extract hrefs from a tags
Unable to extract hrefs from a tags

Time:02-03

I am trying to extract hrefs for two cars(marked in red) from web page. These hrefs are under a tag, to reach them I have tried using CSS_selectors and XPath but with no success. Thank you in advance.

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')

enter image description here

CodePudding user response:

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)

To wait for the visibility of elements use webdriver waits and then to grab the element's hrefs use get_attribute.

Outputs:

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

Import:

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

CodePudding user response:

unsure as to the specific href element that you would like but this gets the href of the tag for the items you have highlighted

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'))

gets

https://www.akrapovic.com/en/car/products/Ferrari/458-Italia-458-Spider?brandId=20&modelId=70
  •  Tags:  
  • Related