My VBA code is not able to select the second drop down option once the first drop down is selected. Not sure why one dropdown is loading and second is not responding as per below code? Appreciate if you could help on fixing this. Regards
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim commodityStr As String
Dim commodityObj As HTMLObjectElement
Dim commodityCodes As IHTMLElementCollection
Dim codeCounter As Long
Dim EDateStr As String
Dim EDateObj As HTMLObjectElement
Dim EdateCodes As IHTMLElementCollection
Dim i As Integer
commodityStr = "MADHYA PRADESH"
EDateStr = "REWA"
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate "http://hydro.imd.gov.in/hydrometweb/(S(ryta1dvaec5pg03bdnxa5545))/DistrictRaifall.aspx"
While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend
Set HTMLDoc = IE.document
End With
Set commodityObj = HTMLDoc.getElementById("listItems")
For codeCounter = 0 To commodityObj.Length - 1
If commodityObj(codeCounter).innerText = commodityStr Then
commodityObj.Value = commodityObj(codeCounter).Value
commodityObj.Focus
commodityObj.FireEvent ("onchange")
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: Wend
Exit For
End If
Next
Set EDateObj = HTMLDoc.getElementById("DistrictDropDownList")
For codeCounter = 0 To EDateObj.Length - 1
If EDateObj(codeCounter).innerText = EDateStr Then
EDateObj.Value = EDateObj(codeCounter).Value
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: Wend
commodityObj.Focus
commodityObj.FireEvent ("onchange")
Exit For
End If
Next
You need a timed loop to allow for that execution of the onchange event. Also, you can make use of css attribute = value selectors to remove loops when targeting elements. It is likely the page does an XHR request so inspect via dev tools to see if that is the case.
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
Dim ie As InternetExplorer, ele As Object, t As Date
Const MAX_WAIT_SEC As Long = 5
Dim commodity As String, iDate As String
commodity = "MADHYA PRADESH"
iDate = "REWA"
Set ie = New InternetExplorer
With ie
.Visible = True
.Navigate2 "http://hydro.imd.gov.in/hydrometweb/(S(3qitcijd521egpzhwqq3jk55))/DistrictRaifall.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("[value='" & commodity & "']").Selected = True
.document.querySelector("[name=listItems]").FireEvent "onchange"
t = Timer
Do
On Error Resume Next
Set ele = .document.querySelector("[value='" & iDate & "']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
ele.Selected = True
.document.querySelector("#GoBtn").Click
Else
Exit Sub
End If
Stop
.Quit
End With
End Sub
Related
Edit: Thank your DearDeer for the solution
'GRV website copy and collect hyperlink
Sub Get_HyperLink1()
Dim ie As InternetExplorer
Application.ScreenUpdating = False
Set ie = New InternetExplorer
ie.Visible = True
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim http As New XMLHTTP60, html As New HTMLDocument
Dim nodeRaceResultsTable As HTMLHtmlElement
Dim nodeTr As HTMLHtmlElement
Dim nodeDiv As HTMLHtmlElement
Dim Element1 As HTMLHtmlElement
Dim node1 As HTMLHtmlElement
Dim currentUrl As String
With ie
ie.Visible = True
The website below is where I want the VBA to navigate
ie.Navigate "https://fasttrack.grv.org.au/Meeting/Search?MeetingDateFrom=22%2F04%2F2020&MeetingDateTo=22%2F04%2F2020&Status=&TimeSlot=&DayOfWeek=&DisplayAdvertisedEvents=false&AllTracks=True&SelectedTracks=AllTracks&searchbutton=Search"
Do Until .readyState = 4: DoEvents: Loop
End With
I'm trying to get the hyperlinks with the VBA elements below
For Each nodeRaceResultsTable In html.getElementsByClassName("search-results")
For Each nodeTr In nodeRaceResultsTable.getElementsByTagName("tr")
With nodeTr.getElementsByTagName("td")
The part below is where I want this VBA to grab the hyperlink and print it on the excel sheet
ws.Cells(5, 5) = .Item(1).getElementsByTagName("a")(0).href
End With
Next
Next
ie.Quit
Set ie = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub
Try something like this:
'GRV website copy and collect hyperlink
Sub Get_HyperLink1()
Dim IE As New InternetExplorer
With IE
.Visible = True
.navigate "https://fasttrack.grv.org.au/Meeting/Search?MeetingDateFrom=22%2F04%2F2020&MeetingDateTo=22%2F04%2F2020&Status=&TimeSlot=&DayOfWeek=&DisplayAdvertisedEvents=false&AllTracks=True&SelectedTracks=AllTracks&searchbutton=Search"
While .Busy Or .readyState < 4: DoEvents: Wend
Dim aTag As Object, i As Long
Set aTag = IE.document.querySelectorAll(".search-results [href]")
For i = 0 To aTag.Length - 1
ActiveSheet.Cells(i + 1, 1) = aTag.Item(i)
Next i
IE.Quit
End With
End Sub
Run time error "70" while VBA is running.
Sometime the code runs smooth but sometime does not. Wondering if there is more reliable code for proceeding. It always stop in If link.innerHTML = "Balance Sheet" Then end if
Public Sub Get()
Dim ie As Object
Dim URL As String, link As Object, alllinks As Object
Dim eRowa As Long, eRowb As Long, eRowc As Long
Dim var As Object
Set var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1)
URL = "https://www.marketwatch.com/investing/stock/" & var & "/financials"
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
Set alllinks = ie.document.getElementsByTagName("a")
For Each link In alllinks
If link.innerHTML = "Balance Sheet" Then
link.Click
End If
Next link
While .Busy Or .readyState < 4: DoEvents: Wend
End With
Set ie = Nothing
End Sub
Expect smooth running without error 70
Use a timed loop to wait for presence of a tag. Use an attribute = value css selector with $ ends with operator for faster targeting of the appropriate element
Option Explicit
Public Sub GetInfo()
Dim ie As Object, url As String, link As Object
Dim var As Range, t As Date
Const MAX_WAIT_SEC As Long = 10
Set var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1)
url = "https://www.marketwatch.com/investing/stock/" & var.value & "/financials"
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate2 url
While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer
Do
On Error Resume Next
Set link = .document.querySelector("[href$='/balance-sheet']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While link Is Nothing
If link Is Nothing Then Exit Sub
link.Click
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
I would like to get customer data from VAT number (NIP number in Poland).
I cannot figure out why code indicated below works only in debug mode and when hit F5 but I set breakpoint at the line with "button.click".
When I run it without breakline it doesn't print any data.
Thank you in advance for any advice how to handle this.
Sleep method used in procedure below is as following:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub IE_GetDataFromSite()
Dim IE As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim button As MSHTML.IHTMLElement
Dim row As MSHTML.IHTMLElement
Dim rows As MSHTML.IHTMLElementCollection
Dim cell As MSHTML.IHTMLElement
Set IE = New SHDocVw.InternetExplorer
IE.Visible = False
IE.navigate "https://wyszukiwarkaregon.stat.gov.pl/appBIR/index.aspx"
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("txtNip")
HTMLInput.value = "9542583988"
Set button = HTMLDoc.getElementById("btnSzukaj")
button.Click
Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Sleep (1000)
Set HTMLDoc = IE.Document
Set rows = HTMLDoc.getElementsByClassName("tabelaZbiorczaAltRow")
For Each row In rows
Debug.Print row.innerText, row.className
If row.className = "tabelaZbiorczaAltRow" Then
For Each cell In row.Children
Debug.Print cell.innerText
Next cell
End If
Next row
IE.Quit
End Sub
Allow a short pause after entering number and also loop until table is present
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer, td As Object
Dim tr As Object, table As Object, t As Date
Const MAX_WAIT_SEC As Long = 5
With ie
.Visible = True
.navigate "https://wyszukiwarkaregon.stat.gov.pl/appBIR/index.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("#txtNip").Value = "9542583988"
Application.Wait Now + TimeSerial(0, 0, 1)
.document.querySelector("#btnSzukaj").Click
While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer
Do
DoEvents
On Error Resume Next
Set table = .document.querySelector("table.tabelaZbiorcza")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While table Is Nothing
If Not table Is Nothing Then
For Each tr In table.getElementsByTagName("tr")
For Each td In tr.getElementsByTagName("td")
Debug.Print td.innerText
Next
Next
End If
.Quit
End With
End Sub
I have the code below to login into the website in the code. However, the login button is not being clicked. I am new to Excel VBA, what do you suggest, please?
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub Myenter()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://willemendrees.nl/fn/login/"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.id_username.Value = "amsterdam#willemendrees.nl"
HTMLDoc.all.id_password.Value = "*****"
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
End With
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
You can use a CSS attribute = value selector of [tabindex='3']. This targets the login button by its tabindex attribute whose value is 3.
HTMLDoc.querySelector("[tabindex='3']").Click
Whole thing:
Option Explicit
Public Sub AttemptLogin()
Dim IE As New InternetExplorer
With IE
.Visible = True
.Navigate2 "https://willemendrees.nl/fn/login/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .querySelector("#id_username")
.Focus
.Value = "amsterdam#willemendrees.nl"
End With
With .querySelector("#id_password")
.Focus
.Value = "***"
End With
.querySelector("[tabindex='3']").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
It seems there are two form elements on that html page and the login is the second. Identify, then submit the form.
HTMLDoc.all.id_username.Value = "amsterdam#willemendrees.nl"
HTMLDoc.all.id_password.Value = "*****"
HTMLDoc.getelementsbytagname("form")(1).submit
The index number of a collection of elements is zero-based.
I am trying to login Web page and fetch data however my login details are not getting update, i have tried all possibilities code from your forum, nothing is working for me
Below is my code, am getting attached error
Sub test()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://portal.expeditors.com/expo/login"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "username" Then
objCollection(i).Value = "bom-sumand"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
'Clean up
Set ie = Nothing
End Sub
I would use the available ids rather than looping to find the input boxes and sign in. These are much faster selector methods. You can add a .Focus. Also, swop InternetExplorer for InternetExplorerMeduim in some cases.
If problem continues check your internet settings in case site is blocked.
Open the URL via creating an IE instance direct.
Option Explicit
Public Sub Login()
Dim ie As New InternetExplorer 'InternetExplorerMedium
Const MAX_WAIT_SEC As Long = 5
Dim t As Date, ele As Object
With ie
.Visible = True
.navigate "https://portal.expeditors.com/expo/login"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Do
DoEvents
On Error Resume Next
Set ele = .getElementById("j_username")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
With ele
.Focus
.Value = "bob"
End With
With .getElementById("j_password")
.Focus
.Value = "penny"
End With
.getElementById("signInBtn").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
Macro trying and failing to open a second instance of IE try this.
Sub test()
Dim ie As Object
Dim redURL As String
Dim objCollection As Object
Dim i As Integer
redURL = "https://portal.expeditors.com/expo/login"
On Error Resume Next
Set ie = GetObject(, "InternetExplorer.Application")
If Err Then
Set ie = CreateObject("InternetExplorer.Application")
End If
On Error GoTo 0
ie.Visible = True
ie.Navigate redURL
Do While ie.Busy
Loop
End Sub