以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]网页元素怎么才能判断是否有这个元素呢?我老是出现错误  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=132486)

--  作者:zto001
--  发布时间:2019/3/22 17:53:00
--  [求助]网页元素怎么才能判断是否有这个元素呢?我老是出现错误
\'\'\'
\'赋值单号
Dim dh As String=e.Form.Controls("TextBox1").value
If dh IsNot Nothing Then
\'赋值浏览器
Dim web As System.Windows.Forms.WebBrowser = e.Form.Controls("WebBrowser1").Basecontrol
Do Until web.ReadyState = 4
    Application.DoEvents
Loop
web.Document.GetElementById("searchBillNo").SetAttribute("Value", dh)
\'模拟点击查询
Dim Inputs2 As object = web.Document.GetElementsByTagName("input")
For Each Input2 As object In Inputs2
    If Input2.GetAttribute("value") = "查询"
        Input2.InvokeMember("click")
    End If
Next
\'延迟一秒
Dim sdt As Date = Date.Now
While Date.Now < sdt.AddSeconds(1)
    Application.DoEvents
End While
\'模拟点击查看完整信息
web.Document.GetElementById("operateLog").InvokeMember("click") 
\'模拟点击面单发放
Dim inputs1 = web.Document.GetElementsByTagName("div")
For Each Input1 As object In Inputs1
    If Input1.GetAttribute("data-type") = "waybillGrant"
        Input1.InvokeMember("click")
    End If
Next
\'延迟一秒    
    Dim sdt2 As Date = Date.Now
    While Date.Now < sdt2.AddSeconds(1)
        Application.DoEvents
    End While
\'模拟点击订单信息
For Each Input2 As object In Inputs1
    If Input2.GetAttribute("data-type") = "orderInfo"
        Input2.InvokeMember("click")
    End If
Next
\'延迟两秒
Dim sdt1 As Date = Date.Now
While Date.Now < sdt1.AddSeconds(2)
    Application.DoEvents
End While
\'获取收件人信息
Dim elems As object = web.Document.GetElementsByTagName("table")
Dim trs0 = elems(0).GetElementsByTagName("tr")
Dim tr = trs0(1)
Dim tds1 = tr.GetElementsByTagName("td")
Dim sd As String=tds1(4).Innertext & tds1(5).Innertext \'【因为没有此元素导致错误】
e.Form.Controls("TextBox2").Value=sd
\'获取面单发放
Dim trs1 = elems(1).GetElementsByTagName("tr")
Dim tr1 = trs1(1)
Dim tds2 = tr1.GetElementsByTagName("td")
Dim md As String=tds2(7).Innertext  \'【因为没有此元素导致错误】
e.Form.Controls("TextBox6").Value=md
End If
msgbox("请输入单号!")

想问问这两处怎么改才不会因为没有元素导致出错呢?

--  作者:有点蓝
--  发布时间:2019/3/22 21:05:00
--  
Dim tds1 = tr.GetElementsByTagName("td")
if tds1 isnot nothing andalso tds1.count > 5 then
……有
end if