【问题标题】:Try to get the anchor tag尝试获取锚标签
【发布时间】:2016-10-04 05:58:57
【问题描述】:

我正在尝试获取锚标记,但出现运行时错误,这是我的代码

      Dim html1 As String = WebControl1.ExecuteJavascriptWithResult("document.getElementsByTagName('html')[0].innerHTML")
    Dim doc1 As New HtmlDocument
    doc1.LoadHtml(html1)
    WaitForPageLoad()
    Dim node As HtmlNodeCollection = doc1.DocumentNode.SelectNodes("//a")
    WaitForPageLoad()
    For Each links As HtmlNode In node
        If links.Attributes("href").Value = TextBox2.Text Then
            WebControl1.ExecuteJavascriptWithResult("")
            Application.DoEvents()
            WaitForPageLoad()
        End If

    Next

我不知道我哪里错了

【问题讨论】:

  • 您在 what 行中遇到了哪个错误?
  • NullPointerException at links.Attributes("href").Value
  • 如果出错时在调试器中查看links的值,是否有“href”属性?
  • 不,我没有得到''href''属性,为什么? @安德鲁·莫顿

标签: .net vb.net xpath html-agility-pack


【解决方案1】:

如果links.Attributes("href")Nothing,则没有.Value 属性。

null-conditional operator?. 可以避免这个问题:

If links.Attributes("href")?.Value = TextBox2.Text Then

【讨论】:

  • 还有一个问题是我没有得到所有的锚标签
  • @AbhinandanKumar 你真的应该针对另一个问题提出一个新问题。现在,我建议将html1 保存到一个文件中并在调试器中检查links 的值时引用它。仔细看看你得到的和你认为应该得到的有什么不同。 (另外,我看不出第二个 WaitForPageLoad() 的原因。)
猜你喜欢
  • 1970-01-01
  • 2012-08-29
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2015-10-07
相关资源
最近更新 更多