为什么以下测试失败?
func TestGetFirstElementHtml(t *testing.T) {
test := `<speak><p>My paragraph</p></speak>`
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(test))
var childrenHtml []string
doc.Find("speak").Children().Each(func(i int, s *goquery.Selection) {
html, _ := s.Html()
childrenHtml = append(childrenHtml, html)
})
if childrenHtml[0] != "<p>My paragraph</p>" {
t.Fatalf("First element html is not valid: '%s'", childrenHtml[0])
}
}
这是测试结果:
=== FAIL: . TestGetFirstElementHtml (0.00s)
main_test.go:45: First element html is not valid: 'My paragraph'
换句话说,如果我无法预测该孩子是哪种 html 元素,我该如何检索第一个孩子的完整 HTML?