If I do an expression like (//*/div) it works the first time but subsequent calls with the same expression return nil. They always return the same result however if i simply drop the paranthesis. This is problematic because I need the paranthesis do pick an element by its index (//*/div)[1]. This seems to have been introduced in the fix for #42 which was done in version 1.2.0 of the xpath library that htmlquery uses.
Here's a sample piece of code where it fails.
package main
import (
"fmt"
"strings"
"github.com/antchfx/htmlquery"
"golang.org/x/net/html"
)
func main() {
fmt.Printf("Run 1: %#v\n", getDiv(`(//*/div)`)) // works
fmt.Printf("Run 2: %#v\n", getDiv(`(//*/div)`)) // fails
fmt.Printf("Run 1: %#v\n", getDiv(`//*/div`)) // works
fmt.Printf("Run 2: %#v\n", getDiv(`//*/div`)) // also works
}
func getDiv(selector string) *html.Node {
s := `<html><head></head><body><div>a</div></body>`
doc, err := htmlquery.Parse(strings.NewReader(s))
if err != nil {
panic(err)
}
return htmlquery.FindOne(doc, selector)
}
If I do an expression like
(//*/div)
it works the first time but subsequent calls with the same expression return nil. They always return the same result however if i simply drop the paranthesis. This is problematic because I need the paranthesis do pick an element by its index(//*/div)[1]
. This seems to have been introduced in the fix for #42 which was done in version 1.2.0 of the xpath library that htmlquery uses. Here's a sample piece of code where it fails.