【问题标题】:VB.NET Webbrowser, htmlelement getting a textbox by element that has no IDVB.NET Webbrowser,htmlelement按没有ID的元素获取文本框
【发布时间】:2017-11-26 23:59:31
【问题描述】:

使用下面的代码,我正在登录一个网站,然后我需要获取图片中突出显示的文本框。但是,文本框没有 id。我怎样才能进入文本框自动输入信息。Google Inspection of the textbox element

公共子 start_processing()

webBrowser1.Navigate("login to website passing credentials")

Do Until webBrowser1.ReadyState = WebBrowserReadyState.Complete
    Application.DoEvents()
Loop

webBrowser1.Navigate("https://secure.tmhp.com/TexMedConnect/EV/Default.aspx?pn=usercontrols/AcuteCare/EligibilityVerification")

Do Until webBrowser1.ReadyState = WebBrowserReadyState.Complete
    Application.DoEvents()
Loop

WebBrowser1.Document.GetElementById("fdosDatePicker").SetAttribute("Eligibility From Date", "11/22/2017")

'Also have been trying to get this to work but still cannot establish the input box

Dim allelements As HtmlElementCollection = webBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
    If webpageelement.GetAttribute("class") = "igte_TMCEditInContainer igte_TMCEditInContianer" Then
        MessageBox.Show("in if")
        webpageelement.SetAttribute("value", "example")
    End If
Next

end sub 

【问题讨论】:

  • 删除所有的 DoEvents,它们是代码异味。您也可以连接到WebBrowser.DocumentCompleted 事件并在其中执行您的代码。最后一点,上面的代码没有反映你想要做什么,请更新。
  • 我从您的问题中删除了 ASP.NET 标签。如果您使用的是WebBrowser 控件,那么它是一个 Windows 窗体项目,因此与 ASP.NET 没有任何关系。
  • 你显然必须对输入标签有所了解才能对它做任何事情。你对它了解多少?你知道这是一个开始的输入标签。您是否搜索过如何访问输入标签?你还知道什么?
  • 大家好,感谢您的积极反馈。我已经更新了代码来说明我想要做什么。对 vb.net 来说还是很新的,所以请原谅我的代码有异味 @Codexer 我用 if 语句尝试过,页面无法完全加载。

标签: vb.net webbrowser-control


【解决方案1】:

或多或少你在做什么。

您可以解析 WebBrowser.Document 中的元素,然后设置 将您关心的人赋予新的价值。

我想这里有 1 个或多个具有提交按钮的表单。
这是标准。如果您的情况有所不同,则可以轻松调整。

Dim _found As Boolean = False

If webBrowser1.Document.Forms.Count > 0 Then
   For Each _form As HtmlElement In _htmlpage.Forms

      'Get all <input> elements in the current Form
      Dim _inputs As HtmlElementCollection = _form.GetElementsByTagName("INPUT")

      'Start from the last element of the collection
      For x As Integer = _inputs.Count - 1 To 0 Step -1

         'A submit element has been found, so look for the "wanted" element
         If _inputs(x).GetAttribute("type") = "submit" Then
            For Each _input As HtmlElement In _inputs

              'When found, set its new value
               If _input.GetAttribute("class") = "igte_TMCEditInContainer" Then
                  _input.SetAttribute("value", "example")
                  _found = True
                  Exit For
               End If
            Next
            Exit For
         End If
      Next
      If _found = True Then Exit For
   Next
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-07
    • 2018-03-30
    • 1970-01-01
    • 2011-05-19
    • 2015-07-21
    • 1970-01-01
    • 2012-07-06
    • 2010-12-11
    相关资源
    最近更新 更多