VBA在IE中提交表单后直接跳过代码

VBA在IE中提交表单后直接跳过代码,vba,internet-explorer,excel,web-scraping,Vba,Internet Explorer,Excel,Web Scraping,目前,我有两段代码分别工作,但当一起使用时,它们不能正常工作 第一个代码要求用户输入存储的信息。然后,它导航到正确的网页,在该网页中,它使用存储的用户输入信息通过填写和提交表单进行导航。它到达了正确的地方 第二个代码通过ie.navigate“insert URL here”使用特定的URL导航到与第一个代码相同的位置。然后,它抓取URL数据并将其存储在新创建的工作表中。它正确地做到了这一点 当合并它们时,我将第二个代码中的导航段替换为第一个代码,但是它只存储60个URL中的前5个,就好像在抓取

目前,我有两段代码分别工作,但当一起使用时,它们不能正常工作

第一个代码要求用户输入存储的信息。然后,它导航到正确的网页,在该网页中,它使用存储的用户输入信息通过填写和提交表单进行导航。它到达了正确的地方

第二个代码通过
ie.navigate“insert URL here”
使用特定的URL导航到与第一个代码相同的位置。然后,它抓取URL数据并将其存储在新创建的工作表中。它正确地做到了这一点

当合并它们时,我将第二个代码中的导航段替换为第一个代码,但是它只存储60个URL中的前5个,就好像在抓取数据之前它没有完全加载页面一样。它似乎直接跳过
ie.document.forms(0)之后的代码。提交
,这应该等待页面加载,然后再继续抓取

额外信息:该按钮未定义,因此我不能直接单击它,因此我必须使用
ie.document.forms(0)。提交

我希望代码执行的操作的摘要:

 request user input
    store user input
    open ie
    navigate to page
    enter user input into search field
    select correct search category from listbox
    submit form
  'problem happens here
    scrape url data
    store url data in specific excel worksheet
合并代码:

    Sub extractTablesData()

                 Dim ie As Object, obj As Object
                 Dim Var_input As String
                 Dim elemCollection As Object
                 Dim html As HTMLDocument
                 Dim Link As Object
                 Dim erow As Long


                ' create new sheet to store info
                Application.DisplayAlerts = False
                ThisWorkbook.Sheets("HL").Delete
                ThisWorkbook.Sheets.Add.Name = "HL"
                Application.DisplayAlerts = True

                 Set ie = CreateObject("InternetExplorer.Application")

                 Var_input = InputBox("Enter info")

                 With ie

                 .Visible = True
                 .navigate ("URL to the webpage")

                 While ie.readyState <> 4
                 DoEvents
                 Wend

                'Input Term 1 into input box
                ie.document.getElementById("trm1").Value = Var_input


                'accessing the Field 1 ListBox
                For Each obj In ie.document.all.Item("FIELD1").Options

                        If obj.Value = "value in listbox" Then

                            obj.Selected = True

                        End If

                    Next obj

                ' button undefined - using this to submit form
                ie.document.forms(0).submit
'----------------------------------------------------------------                 
        'seems to skip this part all together when merged           

        'Wait until IE is done loading page
        Do While ie.readyState <> READYSTATE_COMPLETE

        Application.StatusBar = "Trying to go to website…"
        DoEvents
        Loop

'----------------------------------------------------------------        
        Set html = ie.document
        Set ElementCol = html.getElementsByTagName("a")

        For Each Link In ElementCol
        erow = Worksheets("HL").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Cells(erow, 1).Value = Link
        Cells(erow, 1).Columns.AutoFit
        Next


        Application.StatusBar = “”
        Application.ScreenUpdating = True

               End With
            End Sub
Sub-extractTablesData()
尺寸ie作为对象,obj作为对象
作为字符串的Dim Var_输入
作为对象的暗元素集合
将html设置为HTMLDocument
作为对象的Dim链接
暗淡如长
'创建新工作表以存储信息
Application.DisplayAlerts=False
此工作簿。工作表(“HL”)。删除
ThisWorkbook.Sheets.Add.Name=“HL”
Application.DisplayAlerts=True
设置ie=CreateObject(“InternetExplorer.Application”)
变量输入=输入框(“输入信息”)
与ie
.Visible=True
.导航(“指向网页的URL”)
而ie.readyState 4
多芬特
温德
'将术语1输入到输入框中
ie.document.getElementById(“trm1”).Value=Var\u输入
'访问字段1列表框
对于ie.document.all.Item(“FIELD1”)选项中的每个obj
如果obj.Value=“列表框中的值”,则
所选对象=真
如果结束
下一个obj
'按钮未定义-使用此按钮提交表单
ie.document.表格(0).提交
'----------------------------------------------------------------                 
'合并时似乎会一起跳过此部分
'等待IE完成加载页面
在ie.readyState readyState\u完成时执行此操作
Application.StatusBar=“正在尝试访问网站…”
多芬特
环
'----------------------------------------------------------------        
设置html=ie.document
Set ElementCol=html.getElementsByTagName(“a”)
对于ElementCol中的每个链接
erow=工作表(“HL”)。单元格(Rows.Count,1)。结束(xlUp)。偏移量(1,0)。行
单元格(erow,1)。值=链接
单元格(erow,1).Columns.AutoFit
下一个
Application.StatusBar=“”
Application.ScreenUpdating=True
以
端接头

我在这个问题上已经坚持了很长一段时间,自己还没有找到任何解决办法,所以我正在寻求帮助。任何帮助都将不胜感激

您提到您认为网站可能没有完全加载。这是一个常见的问题,因为网页上有更多的动态元素。处理此问题的最简单方法是插入行:

Application.Wait Now + Timevalue("00:00:02")
这将迫使代码再暂停2秒钟。在等待页面加载的代码下面插入这一行,这将使Internet Explorer有机会进行备份。根据网站和连接的可靠性,我建议在任何地方调整此值,最长可达5秒


大多数网站似乎需要像这样的额外等待,所以当事情不能按预期工作时,可以使用方便的代码来记住。希望这能有所帮助。

我用一种完全不同的方法解决了这个问题。我使用了一个带字符串的查询表去我想去的地方

 Sub ExtractTableData()
    Dim This_input As String
    Const prefix As String = "Beginning of url"
    Const postfix As String = "end of url"

    Dim qt As QueryTable
    Dim ws As Worksheet

    Application.DisplayAlerts = False
    ThisWorkbook.Sheets("HL").Delete
    ThisWorkbook.Sheets.Add.Name = "HL"
    Application.DisplayAlerts = True


    This_input = InputBox("enter key info to go to specific url")
    Set ws = ActiveSheet

    Set qt = ws.QueryTables.Add( _
    Connection:="URL;" & prefix & This_input & postfix, _
    Destination:=Worksheets("HL").Range("A1"))

    qt.RefreshOnFileOpen = True
    qt.WebSelectionType = xlSpecifiedTables
'qt.webtables is key to getting the specific table on the page     
    qt.WebTables = 2

    qt.Refresh BackgroundQuery:=False



    End Sub

谢谢你的回复。有了这段代码,它就完美地等待了,但代码仍然没有抓住所有的链接。既然事实证明这不是根本的问题,那么是时候换个角度了。查看网页上的源代码,它似乎是从第一个或最后一个表(都包含相同的信息)获取链接,但不是从第二个表获取链接,第二个表是我要查找的数据所在。