WebScraping - Parsel: XPath in python
Here is a way to get those elements you're looking for using BeautifulSoup instead of Parsel -- you can adapt the code to Parsel if you are so inclined, and you can edit the class content as well to suit your usecase:
from bs4 import BeautifulSoup as bs
import requests

headers= {
    'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
}

r = requests.get("https://medium.com/", headers=headers)
soup = bs(r.text, 'html.parser')
elements = soup.select('h2[class^="by j"]')
for x in elements:
    print(x.text)

Result in terminal:
No One Really Tells You The Hardest Part Of Getting Older
A good day with Jeff
4 Secrets of Emotionally Stable People
Thought experiment in the National Library of Thailand
Unnatural Keys
Business Model Generation & Playing to Win
On the nature of elegance
It’s Time to Re-Design How We Think
Report from a Relationship
The Secret to Good Photography
A good day with Jeff
How I Turned My Company’s Docs into a Searchable Database with OpenAI
Thought experiment in the National Library of Thailand
What We’re Reading: Always Take a Peek at the Comments
A Contrarian Bet Against Generative AI: The Value of Being Uniquely Human
Why an Engineering Manager Should Not Review Code

BeautifulSoup documentation can be found here.

I am trying to scrape medium.com
I am using the following code:
from parsel import Selector
def get_trending(html):
    selector = Selector(text=html)

    text = selector.xpath("//*[@id='root']/div/div[4]/div[1]/div/div/div/div[2]/div/div[1]/div/div/div[2]/div[2]/a/div/h2")
    # h2 = text.xpath("//h2/text()")
    print(text)

response = requests.get("https://medium.com")
opponents = get_trending(response.text)
opponents

For some reason, it is giving an empty list as a result. I tried it with another h2 and I got the result I wanted. What might be the problem?
Here is the screenshot when I try the xpath in the inspect tool it is working as shown in the image below.

