按 ID 读取网站上项目的名称



我正在使用Webbrowser1.Document.GetElementById命令。

Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
    Dim Col As String = ""
    Try
        Col = wb1.Document.GetElementById("_arialId_118.row.cells.displayName").Name.ToString()
    Catch ex As Exception
        MessageBox.Show(MessageBox.Show(ex.ToString()))
    End Try
    MessageBox.Show(Col)
End Sub

我知道 ID 是正确的,因为我仔细检查了它。但是,我仍然得到一个空引用异常。这可能是什么原因?

编辑:文档示例:

<button type="button" class="_ph_I6 _ph_T6 o365button" tabindex="-1" role="group" id="_ariaId_118.row.cells"><div class="_ph_J6"> <div class="_ph_U6 _ph_R6">  </div> <div class="_ph_P6 _ph_R6 ms-font-color-neutralPrimary" tabindex="-1"> <span picturesize="50"> <span class="_ph_Q6 ms-font-weight-semilight PersonaPaneLauncher"><div class="_pe_d _pe_92 _pe_K _pe_i" role="button" tabindex="0" aria-expanded="false" aria-label="Persona-Karte" aria-haspopup="true">  <span class="_pe_j"> <div class="_pe_o2"> <span class="_pe_t _pe_R1 owaimg _pe_l2 _pe_q2 presenceJellyBeanBase presenceJellyBeanSmallBase presenceJellyBeanOffline" style=""> </span> </div> </span> <span class="_pe_n"> <div class="_pe_D1"><div style="display: none;"></div><div><div><div class="nonDraftPart _pe_12 _pe_j2 _pe_v2" style="background-size: cover; background-position: center center; background-color: rgb(43, 87, 151);">AA</div></div><div style="display: none;"></div></div></div> </span> <div style="display: none;"></div> </div></span> </span> </div> <span class="_ph_R6"> <span class="bidi _ph_S6 _ph_O6 disableTextSelection" tabindex="-1" role="button" id="_ariaId_119.row.cells.displayName" title="Abba Alessio">Abba Alessio</span> </span> <span class="_ph_R6"> <span class="bidi _ph_S6 _ph_O6 disableTextSelection" role="button" tabindex="-1" id="_ariaId_119.row.cells.emailAddress" title="Alessio_Abba@vs.sluz.ch">Alessio_Abba@vs.sluz.ch</span> </span> <span class="_ph_R6"> <span class="bidi _ph_S6 _ph_O6 disableTextSelection" role="button" tabindex="-1" id="_ariaId_119.row.cells.jobTitle" title="VSAD-Schüler">VSAD-Schüler</span> </span> <span class="_ph_R6"> <span class="bidi _ph_S6 _ph_O6 disableTextSelection" role="button" tabindex="-1" id="_ariaId_119.row.cells.officeLocation" title="VS">VS</span> </span> </div></button>

请尝试以下代码:

Col = wb1.Document.GetElementById("_ariaId_119").Name.ToString ()

最新更新