Excel VBA navigation of html elements - vba

I am working on a code that uses VBA-excel to navigate to a website. Once I have logged into the website, I am not able to interact with any of the html elements within the page. I need to be able to click on some check boxes and enter a date range into a field. Here is the code that I have. It works when I open a new window but I cant get it to work within the original ie window. I would like this process to be done in one window if possible. Any feedback is greatly appreciated.
Private Sub CommandButton1_Click()
Dim yesterday As String
yesterday = Date - 1
Dim RowCount As Integer
Dim x As Integer
Dim nik As Integer
'Opens Internet Explorer
Dim Shellwins As SHDocVw.ShellWindows
Dim Shellie As SHDocVw.InternetExplorer
Set Shellwins = New SHDocVw.ShellWindows
Dim ie1 As InternetExplorer
Strurl = "father.com"
Set ie1 = New InternetExplorer
ie1.Visible = True
ie1.navigate Strurl
Do While ie1.readyState <> 4 And ie1.Busy
DoEvents
Loop
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
'MsgBox htmlel.Name
If htmlel.ID = "ctl00_Body_txtEmailAddress" Then
htmlel.Value = "dad#dad.com"
End If
Next
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
'MsgBox htmlel.Name
If htmlel.ID = "ctl00_Body_txtPassword" Then
htmlel.Value = "mom"
End If
Next
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_btnLogon" Then
htmlel.Click
End If
Next
Strurl = "url.com"
Set ie2 = New InternetExplorer
ie2.Visible = True
ie2.navigate Strurl
ie1.Quit
Do While ie2.readyState <> 4 And ie2.Busy
DoEvents
Loop
Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_txtStartDate" Then
htmlel.Value = yesterday
End If
Next
Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_txtEndDate" Then
htmlel.Value = yesterday
End If
Next

Related

Scraping Data from Website using vba doesn´t work

I want to create a list of player names scraping a website. The Internet Explorer starts but I get an run time error "438" - Object does not support this property or method.
Structure of webpage
My coding is as follows:
Option Explicit
Sub Kickbase()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLdoc As MSHTML.HTMLDocument
Dim HTMLPlayers As MSHTML.IHTMLElementCollection
Dim HTMLPlayer As MSHTML.IHTMLElement
Dim i As Integer
Dim HTMLfirstName As Object
Dim firstName As String
IE.Visible = True
IE.Navigate "https://play.kickbase.com/transfermarkt/kaufen"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now + TimeValue("0:00:10"))
Set HTMLdoc = IE.Document
Set HTMLPlayers = HTMLdoc.getElementsByClassName("players")
For i = 0 To HTMLPlayers(0).getElementsByClassName("firstName").Length - 1
Set HTMLfirstName = HTMLPlayers(0).getElementsByClassName("firstName")
If Not HTMLfirstName Is Nothing Then
firstName = Trim(HTMLfirstName.innerText)
Else
firstName = "no_value"
End If
Debug.Print firstName
Next i
End Sub
I have activated the following libraries:
Since it's not possible to test the website on my own, the code below might not be the best way to do it but it should work:
Sub Kickbase()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLdoc As MSHTML.HTMLDocument
Dim HTMLPlayers As Object
Dim i As Integer
Dim firstName As String
IE.Visible = True
IE.navigate "https://play.kickbase.com/transfermarkt/kaufen"
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:10"))
Set HTMLdoc = IE.document
Set HTMLPlayers = HTMLdoc.getElementsByClassName("playerName")
For i = 0 To HTMLPlayers(0).getElementsByClassName("firstName").Length - 1
firstName = Trim$(HTMLPlayers(0).getElementsByClassName("firstName")(i).innerText)
If firstName = vbNullString Then firstName = "no_value"
Debug.Print firstName
Next i
'=== Optional depending on your use case, remember to close IE or else it will remain there ===
'IE.Quit
'Set IE = Nothing
End Sub

Href VBA clicking

I've been trying many codes to click the right button and open the page to scrape the data.
I was able to have the form filled and then click on submit, but the second part I can't seem to put to work. I want it to click on the href with has the same name that I searched on the form
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub CETIP()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www2.cetip.com.br/TitulosCRI"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.Navigate sURL
oBrowser.Visible = True
Do
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.document
HTMLDoc.all.ddlCodigoIF.Value = "08L0002118"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Err_Clear:
Resume Next
End Sub

Login into a Website

I have read a number of posts related to this topic, tried a number of different methods and still can't get it to work.
The macro is working for the user name and password but login not working..
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub RepsolLogin()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim form As HTMLFormElement
MyURL = "https://login.repsol.com/es/Landing/AuthnPage?returnUrl=https://www.repsol.com/es_es/"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
DoEvents
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
Do While HTMLDoc.getElementById("gigya-login-form") Is Nothing
DoEvents
Loop
Set form = HTMLDoc.getElementById("gigya-login-form")
form.all.UserName.Value = "xxx#xxx.com" 'Enter your email id here
form.all.Password.Value = "password" 'Enter your password here
form.submit
End Sub
Can anyone have a look an tell me how I must have the macro?

Error While using Macro to add google's first image link to excel

I'm using the below Code to input Google's first images link in B1
for certain values in A1.
Public Sub Test()
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim imgElements As IHTMLElementCollection
Dim imgElement As HTMLImg
Dim aElement As HTMLAnchorElement
Dim N As Integer, I As Integer
Dim Url As String, Url2 As String
Dim LastRow As Long
Dim m, sImageSearchString
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LastRow
Url = "http://www.google.co.in/search?q=" & Cells(I, 1) & "&source=lnms&tbm=isch&sa=X&rnd=1"
Set IE = New InternetExplorer
With IE
.Visible = False
.Navigate Url 'sWebSiteURL
Do Until .readyState = 4: DoEvents: Loop
'Do Until IE.document.readyState = "Complete": DoEvents: Loop
Set HTMLdoc = .document
Set imgElements = HTMLdoc.getElementsByTagName("IMG")
N = 1
For Each imgElement In imgElements
If InStr(imgElement.src, sImageSearchString) Then
If imgElement.ParentNode.nodeName = "A" Then
Set aElement = imgElement.ParentNode
Url2 = imgElement.src
N = N + 1
End If
End If
Next
Cells(I, 2) = Url2
IE.Quit
Set IE = Nothing
End With
Next
End Sub
however I'm receiving the below error, can you please advise?
I'm using Windows 10, Excel 365
In VBA Menu - Tools - References - tick MS Internet Controls.
Or
Using Late Binding
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")

Web scraping with Excel and VBA

I am trying to extract the team names but i get the "Run time error 424 Object required" on this line
Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")
, if anyone could point me in the right direction it would be nice.
Sub useClassnames()
Dim lists As IHTMLElementCollection
Dim anchorElements As IHTMLElementCollection
Dim ulElement As HTMLUListElement
Dim liElement As HTMLLIElement
Dim row As Long
Dim ie As InternetExplorer
Set ie = New InternetExplorer
With ie.navigate "https://www.unibet.ro/betting#filter/all/all/all/all/in-play"
.Visible = True
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")
row = 1
For Each ulElement In lists
For Each liElement In ulElement.getElementsByClassName("KambiBC-event-participants")
Set anchorElements = liElement.getElementsByClassName("KambiBC-event-participants__name")
If anchorElements.Length > 0 Then
Cells(row, 1) = anchorElements.Item(0).innerText
row = row + 1
End If
Next liElement
Next ulElement
End Sub
Error 424 is an Object Required Error. What is Html in Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")?
What you need is
Set lists = ie.document.getElementsByClassName("KambiBC-event-item__participants-container")
Code that I used
Sub useClassnames()
Dim lists As IHTMLElementCollection
Dim anchorElements As IHTMLElementCollection
Dim ulElement As HTMLUListElement
Dim liElement As HTMLLIElement
Dim row As Long
Dim ie As InternetExplorer
Set ie = New InternetExplorer
With ie
.navigate "https://www.unibet.ro/betting#filter/all/all/all/all/in-play"
.Visible = True
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Set lists = ie.document.getElementsByClassName("KambiBC-event-item__participants-container")
row = 1
For Each ulElement In lists
For Each liElement In ulElement.getElementsByClassName("KambiBC-event-participants")
Set anchorElements = liElement.getElementsByClassName("KambiBC-event-participants__name")
If anchorElements.Length > 0 Then
Cells(row, 1) = anchorElements.Item(0).innerText
row = row + 1
End If
Next liElement
Next ulElement
End Sub
Screenshot