How do you reference the HTMLSelect object from VBA? - html

I am trying to reference the HTMLSelect object in VBA. Assuming I have a form on the html page and a select control named LB. I can't figure out why I get an error message using the code below.
Dim ieApp As InternetExplorer
Dim ieSelect As HTMLSelect
Dim iePage as HTMLDocument
Set ieApp = New InternetExplorer
ieApp.Visible = True
ieApp.navigate "C:\formA.html"
Do Until ieApp.readyState = READYSTATE_COMPLETE
Loop
Application.Wait (Now() + TimeValue("00:00:05"))
Set iePage = ieApp.document
Set ieSelect = iePage.forms(0).Item("LB")

Hm, not sure, but have you the runat="server" on the in formA.html?

Related

Entering text into webpage searchbox

I am trying to enter text into a searchbox and am running into different errors. Below is my code, can anyone point out to me where i have gone wrong?
Sub GetHTMLDocument()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "http://shopee.sg"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementsByClassName("shopee-searchbar-input__input")
HTMLInput.Value = "Excel VBA"
End Sub
I suggest try to make a test with the code sample below may help to solve the issue.
Sub demo()
Dim URL As String
Dim IE As Object
Dim element As HTMLInputElement
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "YOUR WEB PAGE ADDRESS HERE" 'Add your site address here....
IE.navigate URL
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 2, Now)
Set element = IE.document.querySelector("[class='shopee-searchbar-input__input']")
element.removeAttribute ("aria-label")
element.removeAttribute ("placeholder")
element.Value = "abc"
element.FireEvent ("OnChange")
Set IE = Nothing
End Sub
Output:
Further, you can modify the code example as your requirements.

Input date on date field of a website and click on view report button

I've used VBA but never for scraping website so I'm a beginner.
What I'm trying is to use VBA to go to my SSRS website from work, input the date for the report and run it. I've tried my best to code it but it keeps giving me
Run Error Time 91: Object Variable or With block Variable not set.
Below is my code
Sub AgingReport()
Dim IE As New SHDocVw.InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ReportDate As MSHTML.IHTMLElement
IE.navigate "www.example.com"
IE.navigate
IE.Visible = True
Application.Wait Now + #12:00:04 AM#
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set ReportDate = HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl09_txtValue")
ReportDate.Value = "06/28/2019"
HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl00").Click
End Sub
Below is the HTML. Please let me know how I can approve the code.
HTML For Date Field
<input name="ReportViewerControl$ctl04$ctl09$txtValue" class="null"
id="ReportViewerControl_ctl04_ctl09_txtValue" onkeypress="if
(WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'ReportViewerControl$ctl04$ctl09$txtValue\',\'\')', 0)" type="text" size="28">
Here's the HTML for the the button that is code as input
<input name="ReportViewerControl$ctl04$ctl00" id="ReportViewerControl_ctl04_ctl00" type="submit" value="View Report">
I have changed some of your code and added one HTML Button Element
Hope it helps!
Sub AgingReport()
Dim IE As New SHDocVw.InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ReportDate As MSHTML.IHTMLElement
Dim HtmlButton As MSHTML.IHTMLElement
IE.navigate "www.example.com"
IE.navigate
IE.Visible = True
Application.Wait Now + #12:00:04 AM#
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set ReportDate = HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl09_txtValue")
ReportDate.Value = "06/28/2019"
Set HtmlButton = HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl00")
HtmlButton.Click
End Sub

Scraping data with VBA: Why can't I get access to html elements on certain webpages?

On certain webpages I cannot get access to HTML elements using VBA. What am I doing wrong? For example I have two different pages on the same website.
This code returns number of matches on the page.
Sub Oddsportalmatches()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim Matches As MSHTML.IHTMLElementCollection
IE.Visible = True
IE.Navigate "https://www.oddsportal.com/matches/soccer/"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set Matches = HTMLDoc.getElementsByClassName("name table-participant")
Debug.Print Matches.Length
End Sub
This code returns 0. In fact I cannot get access to any element inside the matches table.
Sub Oddsportalmatches()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim Matches As MSHTML.IHTMLElementCollection
IE.Visible = True
IE.Navigate "https://www.oddsportal.com/predictions/"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set Matches = HTMLDoc.getElementsByClassName("table-participant")
Debug.Print Matches.Length
End Sub

scrape html without id in vba

I'm trying to get month-to-date and year-to-date return values from the website
http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index
into an Excel spreadsheet using VBA. The problem is that there is no "id= " in the code of the page, which I understand would make this process a lot simpler. There is also the matter of which time period (year-to-date or month-to-date) is visible, but I'd be happy with scraping just the MTD values for now.
Here is my code:
Sub Get_Change()
'attempting to scrape Barclay's website
Dim appIE As Object
Dim MyVar As String
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Range("A1").Value = "Working..."
Loop
Set TDelements = appIE.document.getElementsbyClassName("performance-chart-table")
For Each TDelement In TDelements
If TDelement.class = "change" Then
MyVar = TDelement.class.innerText("Value")
End If
Next
Range("A1").Value = MyVar
appIE.Quit
Set appIE = Nothing
End Sub
If I can get a way to set the 'MyVar' variable to the current MTD or YTD value, I'll be done, but I'm having a hard time since there is not a unique identifier for either of these values. Any ideas?
I've recently watched some CSS training videos and I can tell you the CSS selector syntax is powerful and I'd recommend it. This is the same syntax that javascript/web developers use to select elements when using JQuery.
I think you should try using
document.queryselectorall
or in your case because you have drilled in to the document to get the "performance-chart-table" call queryselectorall off of that variable, TDelements.
Documentation at http://www.w3schools.com/jsref/met_document_queryselectorall.asp
and you supply as a parameter a CSS selector string the syntax of which can be found at http://www.w3schools.com/cssref/css_selectors.asp
And I've gone and done it for you....
Sub Get_Change()
'* Tools-References Microsoft HTML Object Library
'attempting to scrape Barclay's website
Dim appIE As Object
Dim MyVar As String
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Range("A1").Value = "Working..."
Loop
Dim htmlDoc As MSHTML.HTMLDocument
Set htmlDoc = appIE.document
Dim TDelements2 As MSHTML.IHTMLElementCollection
Set TDelements2 = htmlDoc.getElementsByClassName("performance-chart-table")
While TDelements2.Length < 1
DoEvents
Application.Wait (Now() + TimeSerial(0, 0, 3))
Set TDelements2 = htmlDoc.getElementsByClassName("performance-chart-table")
Wend
Dim oHTMLTablePerformanceChartTable As MSHTML.HTMLTable
Set oHTMLTablePerformanceChartTable = TDelements2.Item(0)
Dim objChangeCollection As MSHTML.IHTMLDOMChildrenCollection
Set objChangeCollection = oHTMLTablePerformanceChartTable.querySelectorAll(".change")
'Debug.Assert objChangeCollection.Length = 2
Dim objChange2 As Object
Set objChange2 = objChangeCollection.Item(1)
MyVar = objChange2.innerText
'Set TDelements = appIE.document.getElementsByClassName("performance-chart-table")
'
'For Each TDelement In TDelements
' TDelements.querySelectorAll (".change")
' If TDelement.class = "change" Then
' MyVar = TDelement.class.innerText("Value")
'
' End If
'Next
Range("A1").Value = MyVar
appIE.Quit
Set appIE = Nothing
End Sub

VBA to fetch html URL from webpage

I have created Macro which can read all the HTML of provided URL, however I want to fetch all the url from that HTML.
Sub GetHTML_Click()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim j As Integer
Set ie = New InternetExplorer
ie.Visible = True
url = Cells(1, 2)
ie.navigate url
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website ..."
Loop
Application.StatusBar = " "
Set html = ie.document
'Dim htmltext As Collection
Dim htmltext As String
htmltext = html.DocumentElement.innerHTML
'Need to add iURL
Dim htmlurl As IHTMLElement
For Each htmlurl In hmtltext
iurl = htmlurl.toString
Cells(j, 1).Value = CLng(iurl)
j = j + 1
Next
End Sub
I tried to code this to fetch the URLs however its giving "Object Required error"
can anyone please help to modify this macro which will help me to fetch all the URL from HTML page.
I am using www.mini.in website for testing.
Mayur.
Try this :
Dim ie As Object
Dim html As Object
Dim j As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
URL = "google.com"
ie.Navigate URL
Do While ie.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website ..."
Loop
Application.StatusBar = " "
Set html = ie.Document
'Dim htmltext As Collection
Dim htmlElements As Object
Dim htmlElement As Object
Set htmlElements = html.getElementsByTagName("*")
For Each htmlElement In htmlElements
If htmlElement.getAttribute("href") <> "" Then Debug.Print htmlElement.getAttribute("href")
Next