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.
Related
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
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
Need help how to create excel vba code for this
I'll be needing the codes so I can complete my macro.
Thanks in advance
First, you will need to create a reference to:
Microsoft Internet Controls
Microsoft HTML Object Library
In VBE, click Tools > References
Sub clickLink()
Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
Url = "http://UrlToYourLink.com"
With ie
.navigate Url
Do While .Busy Or .readyState < READYSTATE_COMPLETE
DoEvents
Loop
doc = .document
.Visible = True
End With
Dim myBtn As Object
Set myBtn = doc.getElementsByClassName("button rounded")(0)
myBtn.Click
End Sub
The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
Microsoft internet controls are a great way to do this, but if you aren't allowed to add new references, here is another way to go about web scraping.
This methode ain't as 'clean' as Microsoft internet controls and HTML object but it gets the job done.
Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer
'initialize counter
i = 0
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'navigate to the url
ie.navigate "Www.google.com"
'Statusbar shows in the buttom corner of excel
Application.StatusBar = "Loading, please wait..."
'Wait until page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Store all the elements with input tag
Set objCollection = ie.Document.getElementsByTagName("input")
'Go through all input elements
While i < objCollection.Length
'input search field
If objCollection(i).Name = "q" Then
objCollection(i).Value = "Hello World"
End If
'search button
If objCollection(i).Type = "submit" Then
Set objSearchBnt = objCollection(i)
End If
i = i + 1
Wend
objSearchBnt.Click
'Clean up
Set objSearchBnt = Nothing
Set objCollection = Nothing
Set ie = Nothing
'Give excel control over the status bar agian
Application.StatusBar = ""
End Sub
I have a vba code which can upload the data from an excel sheet to a website. However, the code works fine in IE browser 8,but it does not work on a win8 IE browser 11. It gives error 438 ("Object Does'nt support this property or method") and text field remains blank. Here are part of the code:
Dim ie As New InternetExplorer
Dim DOCS As HTMLDocument
ie.Navigate "http://uhs.edu.pk/results/etr2015.php"
ie.Visible = True
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
ie.Document.getElementsName("ROLLNO").Value = "55"
Need help to resolve this problem.
This Should Work for you.
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
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 = "rollno" Then
objCollection(i).Value = "55"
End If
i = i + 1
Wend
'Clean up
Set ie = Nothing
End Sub
If you also want to automate the serach button this should do the trick
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
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 = "rollno" Then
objCollection(i).Value = "55"
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
Code to also fetch data from new page
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
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 = "rollno" Then
objCollection(i).Value = "55"
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
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set objCollection = ie.Document.getElementsByTagName("tr")
Range("A2").Value = Split(objCollection(23).innertext, "Sr. No.")(1)
Range("B2").Value = Split(objCollection(24).innertext, "Roll No.")(1)
Range("C2").Value = Split(objCollection(25).innertext, "Name of the Candidate")(1)
Range("D2").Value = Split(objCollection(26).innertext, "Father's Name ")(1)
Range("E2").Value = Split(objCollection(27).innertext, "Centre")(1)
Range("F2").Value = Split(objCollection(28).innertext, "Entrance Test Marks ")(1)
Range("G2").Value = Split(objCollection(29).innertext, "Total Marks")(1)
'Clean up
ie.Quit
Set ie = Nothing
End Sub
Hope this helps you out :)
Solution for combobox on the other page.
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://result.biselahore.com/"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("Select")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'either one of the methodes below works
'If objCollection(i).Name = "session" Then objCollection(i).Value = 2
If objCollection(i).Name = "session" Then objCollection(i).Item(2).Selected = True
If objCollection(i).Name = "year" Then objCollection(i).Item(2).Selected = True
i = i + 1
Wend
'Clean up
Set ie = Nothing
End Sub
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