使用vba

时间:2019-03-30 13:02:30

标签: excel vba web-scraping

我想在网页文本框中添加值,但出现对象错误。 我无法提供网址,因为它在办公室外无法使用。

以下html代码:文本框:

<input type="text" size="40" name="agent" id="go" value="">

搜索:

<button type="submit">Submit</button>

VBA:

Sub login_page()     



    Dim ieApp As SHDocVw.InternetExplorer
            Dim iedoc As MSHTML.HTMLDocument
            IEApp.Visible = True
            Set ieApp = New SHDocVw.InternetExplorer
    ieApp.Visible = True
    ieApp.navigate "http:/Login"
    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set iedoc = ieApp.document

    with iedoc.forms(0)
    .user.Value = "id"
    .Password.Value = "Pass"
    .submit
    End With
    ieApp.navigate "http:action=view"
    Do While ieApp.Busy: DoEvents: Loop
    'Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

    ieApp.navigate "http:action=run"
    Do While ieApp.Busy: DoEvents: Loop


    ieApp.navigate "http:Report?"
    action=run&category=interfaces&template=base%2Frouterprotocols&section=5"
    Do While ieApp.Busy: DoEvents: Loop
    ieApp.document.parentWindow.execScript "window.location='Go?action=agent'"    

End Sub

2 个答案:

答案 0 :(得分:0)

让我来帮助您

  Dim ie As New InternetExplorer
    Dim elemnts   As Object
    Dim elem      As Object

    ie.Navigate url

    Do While ie.ReadyState = 4: DoEvents: Loop   'Do While.
    Do Until ie.ReadyState >= 3: DoEvents: Call aceitaAlerta(False): Loop  'Do Until
    Set ELEMENTS = ie.Document.all
    For Each elem In ELEMENTS
         on error goto NextOne
         If elem.name = "agente" And elem.id = "go" Then
            elem.Value = "NEWVALUE"
            elem.innerText = "NEWVALUE"
            Exit For
         End If
NextOne:
    on error goto 0
    Next elem

答案 1 :(得分:0)

有一个ID,请使用它

ieApp.document.getElementById("go")

前面的代码

Dim ieApp As InternetExplorer
Set ieApp = New  InternetExplorer
With ieApp
    .Visible = True
    .Navigate2 "URL"

    While .Busy Or .readyState < 4: DoEvents: Wend

    .document.getElementById("go").value = "xyz"
End With