通过VBA从HTML页面获取信息

时间:2016-10-31 11:33:09

标签: html vba

从VBA,我正在尝试访问"用户名"来自网页的单元格,以便我可以输入相应的用户名。

问题是,在页面的HTML代码中,我们有多个具有相同名称的元素,即" LOGON_USERID"我无法弄清楚如何访问正确的。

正如您在图像"部分HTML代码"上看到的那样,我尝试访问的行是突出显示的行,但也有2个其他元素具有相同的在它上面命名。

part of the HTML code

我尝试了很多不同的方法(使用不同的方法或变量类型等),但由于我不熟悉HTML,我无法获得我想要的东西。

Sub  Pum()    

Dim ie As New InternetExplorer
'Dim IEDoc As IHTMLElementCollection
Dim IEDoc As HTMLDocument
Dim name As Object
Dim nameList As HTMLInputElement
Dim WRONGS As DispHTMLElementCollection
Dim Elems As HTMLElementCollection
Dim i As Integer

ie.navigate "thewebsiteinquestion"


ie.Visible = False


WaitIE ie


Set IEDoc = ie.document
'MsgBox IEDoc.DocumentElement.
'Elems = IEDoc.getElementsByTagName("INPUT")
 MsgBox TypeName(IEDoc.getElementById("LOGON_USERID").all)
Set Elems = IEDoc.getElementById("LOGON_USERID")
 'For i = 0 To 5
 MsgBox Elems.Length
'Next i
 For Each name In Elems.Children
 MsgBox name.nodeName
 MsgBox name.Attributes
 MsgBox name.all

Next


'If ((NameStr Isnot Nothing And (NameStr.Length <> 0)) Then
'If NameStr = "LOGON_USERID" Then
'If TypeName(IEDoc.all("LOGON_USERID")) = "HTMLInputElement" Then


    'MsgBox TypeName(IEDoc.all("LOGON_USERID"))
    'Set names = IEDoc.all.Item("text")
    'TypeName (InputUsernameTextzone)
    'Dim Question As IHTMLElement
    'Question = InputUsernameTextzone.parentElement
   'MsgBox TypeName(InputUsernameTextzone.parentElement.getAttribute("name"))
  'InputUsernameTextzone.parentElement

    'CELLULE.value = "qtc2464"



  WaitIE ie


  Set ie = Nothing
  Set IEDoc = Nothing

End Sub

我使用不同的方法尝试了其他两个类似的代码,但我仍然没有结果。希望你能帮助我。

如果您需要更多信息,请与我们联系。

2 个答案:

答案 0 :(得分:0)

其他两个输入元素属于不同类型(它们隐藏),因此您可以使用function GetAjaxCall(url) { return $.ajax({ url: url // Set other properties as needed. }); } $(function () { var arrUrls = ['@Url.Action("Add")', '@Url.Action("StudentLookup")']; var calls = []; $.each(arrUrls,function(a, b) { calls.push(GetAjaxCall(b)); }); $.when.apply($, calls) .then(function() { console.log('all done'); }); }); 与属性querySelector来查找所需元素。

type=text

答案 1 :(得分:0)

我是这方面的新手,但如果这可以帮助任何人,这里是我制作的宏的简化版本:

Sub Access_Puma()

Dim ie As New InternetExplorer
Dim IEDoc As HTMLDocument
Dim userid As HTMLInputElement
Dim userpwd As HTMLInputElement
          ie.navigate "thewebsitetoaccess"

ie.Visible = True


WaitIE ie

  Set IEDoc = ie.document

Set userid = IEDoc.querySelector("input[name='LOGON_USERID'][type='text']")
If Not userid Is Nothing Then
userid.value = "myusername"
Else
MsgBox "LOGON_USERID not found on the page"
End If

Set userpwd = IEDoc.querySelector("input[name='LOGON_PASSWD'][type='password']")
If Not userpwd Is Nothing Then
userpwd.value = "mypassword"
Else
MsgBox "LOGON_PASSWD not found on the page"
End If
End Sub