pagser icon indicating copy to clipboard operation
pagser copied to clipboard

How to select an html element based on his text content?

Open sebbbastien opened this issue 3 years ago • 1 comments

What should be the selector for this kind of html code:

<h2>Firstname</h2>
<p>John</p>
<!--- some random html code, with random h2 tags -->
<h2>Lastname</h2>
<p>Doe</p>

I would like to fill this struct:

type Person struct {
    Firstname string `pagser:"h2[text=Firstname]+p"`
    Lastname  string `pagser:"h2[text=Lastname]+p"`
}

sebbbastien avatar May 10 '21 09:05 sebbbastien

  1. You can define a function write your self.
  2. Try the code:
Firstname string `pagser:"h2:contains('Firstname')+p"`

The selector library: https://github.com/PuerkitoBio/goquery

foolin avatar May 10 '21 13:05 foolin