Parse the HTML, then call doc.Select("#menu > li.featured") to find matching elements without a browser. Requires Go 1.24 or later.
go get recursiveai.org/go/onehtmlpackage main import ( "fmt" "log" "strings" "recursiveai.org/go/onehtml" ) func main() { const html = `<ul id="menu"> <li class="featured">Atlas</li> <li>Compass</li> <li class="featured">Map</li> </ul>` doc, err := onehtml.Parse(strings.NewReader(html)) if err != nil { log.Fatal(err) } items, err := doc.Select("#menu > li.featured") if err != nil { log.Fatal(err) } for _, item := range items { fmt.Println(item.TextContent()) } }
Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onehtml, then go run .; or unzip the example and go run ..
Atlas Map
Only the featured items directly under #menu are printed, in document order. A selector that cannot be compiled returns an error; a valid selector with no matches returns no items.
Variations
For just the first match, replace the selection and loop. doc is the parsed document from the example.
item, err := doc.SelectFirst("#menu > li.featured") if err != nil { log.Fatal(err) } if item.Valid() { fmt.Println(item.TextContent()) }
This prints Atlas.
Related recipes
The short answer
The matches come back in document order. TextContent() gets their text. onehtml-go does not run scripts that might add to the HTML later.
How do you parse an HTML page in Go?
Call onehtml.Parse with the page’s HTML in an io.Reader. The example uses strings.NewReader so you can run it without a network request. An open file or the body of a successful HTTP response is also a reader. Your code opens and closes that input; parsing only builds the document from those bytes. If you’re parsing a template from disk to check its structure, use its generated HTML rather than template source with placeholders.
Selectors work on the tree that the parser builds, not on the source text. The HTML parsing rules can add implied elements or close an element before the next one begins. A direct-child query follows those parent-child relationships. When a selector copied from a template yields no results, check whether its assumed structure matches the parsed tree.
How do you get an element’s value using a CSS selector in Go?
Choose the scope and then the kind of value you need. In #menu > li.featured, #menu identifies the list, > requires an immediate child, and li.featured selects a list item with the class featured. If you remove the class condition, Compass also matches. If you replace > with a space, featured list items in nested lists can match too. These are differences in what nodes match; neither query changes the HTML or applies CSS declarations.
Call TextContent() for descendant text, or Get for an attribute’s value and its presence flag. SelectFirst gives you a single match, with a zero node signaling no match. If you only need to inspect one row, you don’t need to collect every match or index a slice that might be empty.
Why does a CSS selector work in a browser but return nothing in Go?
Start by comparing the input with what the browser shows. onehtml-go does not fetch anything or run JavaScript, so a product list inserted by client-side code is absent unless your input already contains it. Browser interaction is another difference: a selector such as :hover relies on caller-supplied state in Go, not on a live pointer.
A bad selector and a missing element are different cases. Check the query error first, then check whether any nodes matched. If you run the same selector against many documents, compile it once with CompileSelector and use the compiled selector’s All method with each document root. For a single query, Select handles both steps.