VBA Code for Div Class - excel

I've been searching for an answer for days. Please help.
<div class="FBOSearchResultsItemPricingTablePrice">$3.8391</div>
I just need to pull the "$3.8391" from this string within the website and have it populate into a cell in Excel.
Here's what I have so far, all of which works exactly how I need it to. I have gotten the web page to open, log in and search for a location, however, I can't seem to get this one simple number!!
(I obviously changed the username/password in the code to protect my information)
Sub login()
Dim IE As Object
Dim doc As Object
Set IE = CreateObject("InternetExplorer.application")
IE.AddressBar = 0
IE.StatusBar = 0
IE.Toolbar = 0
IE.Visible = True
IE.navigate "https://www.avfuel.com/login"
Do Until Not IE.busy: DoEvents: Loop
Set doc = IE.document
Do While doc.ReadyState <> "complete": DoEvents: Loop
doc.getElementsByName("dnn$ctr2046$Login$Login_AvfuelAuth$txtUserName")(0).Value = "XXXX"
doc.getElementsByName("dnn$ctr2046$Login$Login_AvfuelAuth$txtPassword")(0).Value = "XXXXX"
doc.getElementById("dnn_ctr2046_Login_Login_AvfuelAuth_cmdLogin").Click
Application.Wait (Now + TimeValue("00:00:05"))
doc.getElementsByName("dnn$ctr618$Loader$ctl00$txtAirportSearchInput")(0).Value = "bfl"
doc.getElementById("dnn_ctr618_Loader_ctl00_hlSearchPageAirport").Click
Application.Wait (Now + TimeValue("00:00:05"))
End Sub

Try this
Cells(1,1) = doc.getElementsByClassName("FBOSearchResultsItemPricingTablePrice")(0).innerText

Or use CSS selector
e.g.
[A1] = doc.querySelector(".FBOSearchResultsItemPricingTablePrice").innerText
The "." is for class.

Related

VBA Automation isn't working on new IE tab

It's my first question and I'm new on VBA.
I was trying to run a macro to automatize some procedures on IE. My macro worked very well when opened on diferent windows, but when I tried to run the same macro on tabs it didn't work. It seems (maybe) the macro isn't recognizing the actual tab. Could anyone help me, please?
Sub Test1()
Dim IE As Object
Dim doc As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
For intRow = 1 To 3
If intRow = 1 Then
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp"
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
IE.document.getElementById("principal").Children(4). _
getElementsByTagName("tbody")(0).getElementsByTagName("tr")(5). _
getElementsByTagName("td")(1).getElementsByTagName("font")(0). _
getElementsByTagName("input")(0).Value = "intRow"
Application.Wait DateAdd("s", 2, Now)
End With
Else
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp", 2048&
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
.document.getElementById("principal").Children(4). _
getElementsByTagName("tbody")(0).getElementsByTagName("tr")(5). _
getElementsByTagName("td")(1).getElementsByTagName("font")(0). _
getElementsByTagName("input")(0).Value = "intRow"
Application.Wait DateAdd("s", 2, Now)
End With
End If
Next
End Sub
According to your code and the website, it seems that you want to open multiple tab (with the same URL), and then fill a value in the text box. We should pay attention to the following point:
How to access the web page elements.
How to switch tabs and focus to the new tab.
To access the web page elements, from your web page resource (use F12 developer tools to check it), I notice that the table contains 6 rows, but the last row doesn't contain the input element. So, using your code, I can't find the input element. I found that the input elements contain the class property. So, I suggest you could use the getElementsByClassName method to find the element.
To switch the IE browser tab, we could loop through the opened tabs and compare the URL, and then set focus on the related tab.
More detail steps, please check the following sample code:
Sub Test1()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
Dim intRow As Integer
For intRow = 1 To 3
If intRow = 1 Then
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp"
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
IE.document.getElementsByClassName("basic")(0).Value = "intRow" & intRow
Application.Wait DateAdd("s", 3, Now)
End With
Else
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp", 2048&
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 3, Now)
Set IE = GetIE("https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp")
Application.Wait DateAdd("s", 3, Now)
IE.document.getElementsByClassName("basic")(0).Value = "intRow" & intRow
End With
End If
Next
End Sub
Function GetIE(sLocation As String) As Object
Dim retVal As Object
Dim my_url As String, my_title As String
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
'loop through the window and find the tab
For x = 0 To (IE_count - 1)
On Error Resume Next
'get the location and title
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
'debug to check the value
Debug.Print x
Debug.Print my_url
'find the special tab based on the title.
If my_url Like sLocation Then
Set retVal = objShell.Windows(x)
'IE.Quit 'call the Quit method to close the tab.
'Exit For 'exit the for loop
Else
End If
Next
Set GetIE = retVal
End Function
After running the above script, it will open three tabs and fill a value in the Login input text. Please see the following screenshot:
Reference:
Common VBA Methods & Properties used in web automation

Navigating Date Picker in IE using VBA

I'm trying to automate a report of mine and I can't seem to find the right code for entering the start and end date of my reports. I can't post the link since it's a client web based tool.
I have tried setting up the date via getElementByID and it changes the date but it doesn't extract the data (no data at all). When I don't input any date codes at all, it extracts the data for today with no issues.
I think I need to set up the dates via it's date picker to fully extract the data. I hope someone can walk me through that process.
Commented codes are what I've tried so far and attached pics are for the HTML Files.
Start End Date
Date Picker Start Date
Sub Get_RawFile()
'
'
'
Dim IE As New InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim HTMLselect As HTMLSelectElement
With IE
.Visible = True
.Navigate ("--------------------------")
While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
Set HTMLDoc = IE.document
HTMLDoc.all.UserName.Value = Sheets("Data Dump").Range("A1").Value
HTMLDoc.all.Password.Value = Sheets("Data Dump").Range("B1").Value
HTMLDoc.getElementById("login-btn").Click
While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
Application.Wait (Now + TimeValue("0:00:05"))
Set objButton = HTMLDoc.getElementById("s2id_ddlReportType")
Set HTMLselect = HTMLDoc.getElementById("ddlReportType")
objButton.Focus
HTMLselect.Value = "1"
Set HTMLselectZone = HTMLDoc.getElementById("ddlTimezone")
HTMLselectZone.Value = Sheets("Data Dump").Range("B9").Value
Set subgroups = HTMLDoc.getElementById("s2id_ddlSubgroups")
subgroups.Click
Set subgroups2 = HTMLDoc.getElementById("ddlSubgroups")
subgroups2.Value = "1456_17"
'HTMLDoc.getElementById("dtStartDate").Value = Sheets("Attendance").Range("B6").Value
'HTMLDoc.getElementById("dtStartDate").Click
'HTMLDoc.getElementById("dtEndDate").Value = Sheets("Attendance").Range("X6").Value
'HTMLDoc.getElementById("dtEndDate").Click
HTMLDoc.getElementById("btnGetReport").Click
Application.Wait (Now + TimeValue("0:00:10"))
HTMLDoc.getElementById("btnDowloadReport").Click
End With
End Sub
In Excel, dates are stored as a decimal value.
I guess your HTML expects a string so try to format :
HTMLDoc.getElementById("dtStartDate").Value = Format(Sheets("Attendance").Range("B6").Value, "yyyy-mm-dd")

Excel VB Macro to scrape webpage. Can't code to click html button

I have a short excel macro that is designed to:
1) Open Internet Explorer and navigate to "http://www.puco.ohio.gov/pucogis/address/search.cfm"
2) Fill out a form on that site with data from the excel workbook
3) Click a button to submit the form
4) Scrape some innertext from the website and place it in a cell in the workbook
5) Close Internet Explorer
I can not get step 3 to work. That is, I can not get the click/submit function to work with this website. When the button is clicked the website populates with information specific to the information entered in the form. Everything else in the code is working. I have searched for an answer and tried the submit verses click approach with no luck.
Thanks for you help.
Code below:
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Set HTMLButton = ie.Document.getElementsByTagName("input")(1)
HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
Try this solution, which I found from this answer to a similar question. That answer was not accepted, but I have tested this with your code and seems to be working.
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Call ie.Document.parentWindow.execScript("codeAddress()")
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
If you don't know or can't reasonably anticipate the function call codeAddress(), then you can try something like this to derive it from the button's onclick property:
Dim fn$
fn = HTMLButton.onclick
fn = Mid(fn, InStr(fn, "{"))
fn = Trim(Replace(Replace(Replace(fn, "{", vbNullString), "}", vbNullString), vbLf, vbNullString))
Call ie.Document.parentWindow.execScript(fn)
You can call the JavaScript directly. try this it will work
Instead of:
Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
HTMLButton.Click
use
ie.Document.parentWindow.execScript code:="codeAddress()"
note that IE may prompt you to confirm every run so you may need to
stop showing this message for smooth operation
Private Sub CommandButton1_Click()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
ie.Document.parentWindow.execScript code:="codeAddress()"
'Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
'HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D16") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
thanks also to this article helped me to solve your problem
How to find and call javascript method from vba

Excel VBA & IE 11 - Unable to refresh page after selecting value in a dropdown

I'm trying to get the currency exchange rate offered by WorldRemit for a pair of currencies. I want to change the value in the 'Send From' dropdown list on the top left corner of the webpage. (https://www.worldremit.com/en/South-Africa)
I wasn't able to select the dropdown option using .Selected = True or .Click so have used .SelectedIndex. After selecting the value, I'm not able to trigger the change event that refreshes the page. Would be great if someone can help me figure this out.
Code for navigating to the page:
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://www.worldremit.com/en/South-Africa"
While ie.busy
DoEvents
Wend
Set HTMLdoc = ie.document
Code for selecting option using .SelectedIndex property:
Dim fromSelect As HTMLSelectElement
Set fromSelect = HTMLdoc.getElementById("selectFrom")
optionIndex = Find_Select_Option(fromSelect, "Germany")
If optionIndex >= 0 Then
fromSelect.selectedIndex = optionIndex
fromSelect.FireEvent ("onchange") ' this doesn't work
End If
Function for selecting option (used in the code above):
Function Find_Select_Option(selectElement As HTMLSelectElement, optionText As String) As Integer
Dim i As Integer
Find_Select_Option = -1
i = 0
While i < selectElement.Options.Length And Find_Select_Option = -1
DoEvents
If LCase(Trim(selectElement.Item(i).Text)) = LCase(Trim(optionText)) Then Find_Select_Option = i
i = i + 1
Wend
End Function
Edit #1 : The HTML snippet containing the element in question is
<select id="selectFrom" data-track-field-name="from country" data-track-event="change">...</select>
Give this a try, it's working on my end. Sometimes using jQuery is a bit easier, especially when the page also uses jQuery as it does here.
You can use jQuery by using the execScript function of IE. See below:
Public Sub test()
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://www.worldremit.com/en/South-Africa"
'Wait for the page to load
While .busy Or .readyState <> 4
Application.Wait (Now() + TimeValue("00:00:01"))
DoEvents
Wend
'Use JQuery to find the element based on ID, then make the Selected property true
'Once that is done, call the change event in jQuery
.document.parentWindow.execScript "$('#selectFrom option:contains(Germany)').prop('selected','True')"
.document.parentWindow.execScript "$('#selectFrom option:contains(Germany)').change()"
End With
End Sub
Apparently FireEvent doesn't work all that well with IE 11 so need to use CreatEvent + initEvent + dispatchEvent
Working code snippet below:
Dim fromSelect As HTMLSelectElement
Dim evt As Object
Set evt = HTMLdoc.createEvent("HTMLEvents")
evt.initEvent "change", True, False
Set fromSelect = HTMLdoc.getElementById("selectFrom")
optionIndex = Find_Select_Option(fromSelect, "Germany")
If optionIndex >= 0 Then
fromSelect.selectedIndex = optionIndex
fromSelect.dispatchEvent evt
End If

Excel VBA object required error with IE automation

So I have some code to go to a website and then login. Then I need it to go to the password change URL. Sometimes when I run the code, I get the following error:
Runtime Error 424: Object Required
Sometimes the debugger says it is the first getelementbyid statement, but most times it says the issue is the last three getelementbyid statements. The code is below:
Dim ie As Variant
Dim strURL As String
Sub login()
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "https://minecraft.net/profile"
While ie.Busy
DoEvents
Wend
ie.document.getElementById("username").Value = "ddd"
ie.document.getElementById("password").Value = "ddddddddddd"
Dim htmlForm As HTMLFormElement
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
' **********************************************************************
'IE.Document.getElementById("username").Value = "ddddd"
' IE.Document.getElementById("password").Value = "ddddd"
' IE.Document.getElementById("signin").Click
'**********************************************************************
'Pause while page loads
Application.Wait (Now + #12:00:03 AM#)
ie.navigate "https://minecraft.net/profile/password"
ie.document.getElementById("oldPassword").Value = "oldpass"
ie.document.getElementById("password").Value = "enwapss"
ie.document.getElementById("passwordConfirmation").Value = "enwapss"
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
End Sub
Thanks in advance!
It may be that the website isn't in a ready state, as in the site hasn't fully loaded when it's attempting to input the values.
After
ie.navigate "https://minecraft.net/profile/password"
Try adding
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
This will loop until the webpage has loaded similar, to the way you've done it in your above code with
Application.Wait (Now + #12:00:03 AM#)

Resources