Skip to content

Instantly share code, notes, and snippets.

@migreva
Created June 23, 2017 15:34
Show Gist options
  • Save migreva/bfda4fed23827fc9cbb99fd204de2da2 to your computer and use it in GitHub Desktop.
Save migreva/bfda4fed23827fc9cbb99fd204de2da2 to your computer and use it in GitHub Desktop.
Goquery fails to find element that exists
package main
import (
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func main() {
pageURL := "https://www.whitehouse.gov/forreviewstaff-salaries"
resp, err := http.Get(pageURL)
if err != nil {
panic(err)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
panic(err)
}
// `workingSelector` is a substring of `failingSelector`, and is the last part of the selection that
// successfully finds an element
// workingSelector := "body > *:nth-child(5)"
failingSelector := "body > *:nth-child(5) > *:nth-child(2) > *:nth-child(1) > *:nth-child(2) > *:nth-child(2) > *:nth-child(1) > *:nth-child(1)"
el := doc.Find(failingSelector)
if len(el.Nodes) == 0 {
panic("FAIL: node not found")
}
log.Println("Successfully found element")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment