VBA基本Web查询登录对象错误



我从不同的stackoverflow答案中复制了代码,但对我不起作用。我试图登录站点,然后运行宏。运行时错误91(对象变量或块变量未设置)

'这是代码停止

的地方

.document.all.Item("user").Value = "username"

Private Sub CommandButton1_Click()
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate "http://booneilpropertymax.governmaxa.com/propertymax/rover30.asp?sid=4E131F244E474C2CA87112FA94C83B44"
        Do Until .ReadyState = 4
            DoEvents
        Loop
        .document.all.Item("user").Value = "username"
        .document.all.Item("pass").Value = "password"
        .document.forms(0).SUBMIT
    End With

    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://booneilpropertymax.governmaxa.com/propertymax/tab_land.asp?t_nm=land&l_cr=1&t_wc=|parcelid=03-23-480-003&sid=6ECB98B7DA8F46FCBB04F83F7E225CF4" _
        , Destination:=Range("$J$1"))
        '.CommandType = 0
        .Name = _
        "tab_land.asp?t_nm=land&l_cr=1&t_wc=|parcelid=03-23-480-003&sid=6ECB98B7DA8F46FCBB04F83F7E225CF4"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "13"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End 

您尝试与之交互的元素包含在框架中。另一种方法是使用

https://www.governmax.com/propertymax/user_login.asp?site = login& form = loginform&form = loginform& formelement = 0&formelement = 0&sid = 4b105bbb105bbb10fba474d89d89d3ff256f256c2d56c2d5976

>>

作为您的URL。这是登录页面。在此网页上,以下代码将起作用

ie.document.getElementsByName("user")(0).Value = "abcde"
ie.document.getElementsByName("pass")(0).Value = "12345"
ie.document.getElementsByName("login")(0).Click

稍后添加

框架虽然不是iframe,但表现得像iframe。它提供了一个URL,即导航到,用户可以控制各种元素。捕获此URL使用

new_url = ie.document.getElementsbytagname(" frame")(0).src

甚至稍后

附加

,您的代码看起来像这样

URL_1 = "http://booneilpropertymax.governmaxa.com/propertymax/rover30.asp?sid=4E131F244E474C2CA87112FA94C83B44"
Set ie = CreateObject("InternetExplorer.Application")
With ie
    .Visible = True
    .navigate URL_1
    Do Until .readyState = 4
        DoEvents
    Loop
    new_url = ie.document.getElementsByTagName("frame")(0).src
    ie.navigate new_url
    Do Until .readyState = 4
        DoEvents
    Loop
    ie.document.getElementsByName("user")(0).Value = "username"
    ie.document.getElementsByName("pass")(0).Value = "pass"
    ie.document.getElementsByName("GO")(0).Click
    Application.Wait (Now + TimeValue("0:00:02"))
    'can't check past here because I can't get in - I don't have a pw, etc
    ie.document.getElementsByName("parcelid")(0).Value = (Sheet1.Range("H" & 1))
    ie.document.getElementsByName("go")(0).Click
 End With

所以几点:

1)在您在初始页面上,必须出现捕获new_url的行

2)在页面上,单击按钮的名称是new_url的" go go",带您到

3)您可以使用两个URL来完成工作,您可以使用原始问题中使用的URL,并且我在上面的代码块中使用了。使用此URL,您要操纵的元素在框架中,因此您必须使用new_url方法。或者,您可以使用我在答案的顶部使用的URL,并在答案中使用的URL。使用此URL,您要使用的元素不在框架中,因此您可以将文本直接放入网页中,无需使用new_url方法,实际上,此网页上没有new_url框架。希望这会有所帮助。

对不起,无知。建议的页面将我带到另一个站点,而我仍然遇到对象错误。为什么它会更早起作用,但现在不工作。URL不断变化吗?

私有sub commandton1_click()

new_url = ie.document.getElementsbytagname(" frame")(0).src

Set ie = CreateObject("InternetExplorer.Application")
With ie
    .Visible = True
    .navigate "https://www.governmax.com/propertymax/user_login.asp?site=login&form=loginform&formelement=0&sid=4B105BB10FBA474D89D3FF256C2D5976"
    Do Until .readystate = 4
        DoEvents
    Loop

    ie.navigate new_url
    ie.document.getElementsByName("user")(0).Value = "username"
    ie.document.getElementsByName("pass")(0).Value = "pass"
    ie.document.getElementsByName("login")(0).Click
    Application.Wait (Now + TimeValue("0:00:02"))
    ie.document.getElementsByName("parcelid")(0).Value = (Sheet1.Range("H" & 1))
    ie.document.getElementsByName("go")(0).Click

 End With

最新更新