dosi8657
2019-02-23 12:34
浏览 200
已采纳

如何使用goquery检索子元素的完整HTML?

Why does the following test fail?

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])
    }
}

This is the test result:

=== FAIL: . TestGetFirstElementHtml (0.00s)
    main_test.go:45: First element html is not valid: 'My paragraph'

In other words, how can I retrieve the full HTML of the first child of given that I cannot predict what kind html element that child is?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题