Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to extract hrefs from a tags

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading