Created
June 23, 2017 15:34
-
-
Save migreva/bfda4fed23827fc9cbb99fd204de2da2 to your computer and use it in GitHub Desktop.
Goquery fails to find element that exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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