'XPath: How do I capture the previous element?

I have such a construction

<p>File name</p>
<a href="https://somelink.pdf">Download</a>

I need to capture the link a and its name p using CSS and XPath. I'm trying to do the following, first I find using the CSS selector all files whose href values end in .pdf (a[href$=".pdf"]):

for i in response.css('a[href$=".pdf"]'):
    link = i.css('::attr("href")').get()
    name = i.xpath(?????????)
    print(name, link)

How do I capture the text in the p element using XPath?



Sources

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

Source: Stack Overflow

Solution Source