internet explorer - VBA skipping code directly after submitting form in IE -
currently have 2 pieces of code work separately, when used don't work properly.
the first code asks user input information stored. navigates correct webpage uses stored user input information navigate via filling , submitting form. arrives @ correct place.
the second code uses specific url via ie.navigate "insert url here"
navigate same place first code. scrapes url data , stores in newly created sheet. correctly.
when merging them replace navigation segment second code first code, stores first 5 of 60 urls if hadn't loaded page before scraping data. seems skip code directly after ie.document.forms(0).submit
supposed wait page load before moving on scraping..
extra info: button wasn't defined cannot click had use ie.document.forms(0).submit
summary of want code do:
request user input store user input open ie navigate page enter user input search field select correct search category listbox submit form 'problem happens here scrape url data store url data in specific excel worksheet
the merged code:
sub extracttablesdata() dim ie object, obj object dim var_input string dim elemcollection object dim html htmldocument dim link object dim erow long ' create new sheet 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") ie .visible = true .navigate ("url webpage") while ie.readystate <> 4 doevents wend 'input term 1 input box ie.document.getelementbyid("trm1").value = var_input 'accessing field 1 listbox each obj in ie.document.all.item("field1").options if obj.value = "value in listbox" obj.selected = true end if next obj ' button undefined - using submit form ie.document.forms(0).submit '---------------------------------------------------------------- 'seems skip part when merged 'wait until ie done loading page while ie.readystate <> readystate_complete application.statusbar = "trying go website…" doevents loop '---------------------------------------------------------------- set html = ie.document set elementcol = html.getelementsbytagname("a") 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 end sub
i've been stuck quite time on , haven't found solutions on own i'm reaching out. appreciated!
you mentioned think website might not loaded. common problem because of more dynamic elements on webpage. easiest way handle insert line:
application.wait + timevalue("00:00:02")
this force code pause additional 2 seconds. insert line below code waits page load , give internet explorer chance catch up. depending on website , reliability of connection recommend adjusting value anywhere 5 seconds.
most websites seem require additional waiting this, handy code remember when things don't work expected. hope helps.
Comments
Post a Comment