'Python Selenium: How to pull a link from an element with no href

I am trying to iterate through a series of car listings and return the links to the individual CarFax and Experian Autocheck documents for each listing.

Page I am trying to pull the links from

The XPATH for the one constant parent element across all child elements I am looking for is:

.//div[@class="display-inline-block align-self-start"]/div[1]

I initially tried to simply extract the href attribute from the child <div> and <a> tags at this XPATH: .//div[@class="display-inline-block align-self-start"]/div[1]/a[1]

This works great for some of the listings but does not work for others that do not have an <a> tag and instead include a <span> tag with an inline text link using text element "Get AutoCheck Vehicle History".

That link functions correctly on the page, but there is no href attribute or any link I can find attached to the element in the page and I do not know how to scrape it with Selenium. Any advice would be appreciated as I am new to Python and Selenium.

For reference, here is the code I was using to scrape through the page (this eventually returns an IndexError as only some of the iterations of elements on the list have the <a> tag and the final amount does not match the total amount of listings on the page indicated by len(name)

s = Service('/Users/admin/chromedriver')
driver = webdriver.Chrome(service=s)
driver.get("https://www.autotrader.com/cars-for-sale/ferrari/458-spider/beverly-hills-ca-90210?dma=&searchRadius=0&location=&isNewSearch=true&marketExtension=include&showAccelerateBanner=false&sortBy=relevance&numRecords=100")

nameList = []
autoCheckList = []

name = driver.find_elements(By.XPATH, './/h2[@class="text-bold text-size-400 text-size-sm-500 link-unstyled"]')
autoCheck = driver.find_elements(By.XPATH, './/div[@class="display-inline-block align-self-start"]/div[1]/a[1]')


for i in range(len(name)):
    nameList.append(name[i].text)
    autoCheckList.append(autoCheck[i].get_attribute('href'))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source