VBA: Excel macro code correction - vba

I am looking for a VBA code that can help me to click a button from a web page. I got the below VBA from one of the sites which works as expected.
Sub followWebsiteLink()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim Link As Object
Dim ElementCol As Object
Application.ScreenUpdating = False
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.google.co.in"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website…"
DoEvents
Loop
Set html = ie.document
Set ElementCol = html.getElementsByTagName("a")
For Each Link In ElementCol
If Link.innerHTML = "Google Pixel 2" _ 'Or u can use "Advertising"
Then
Link.Click
End If
Next Link
Set ie = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub
But if I replace it with the site name I want, it wont work.
**The changes I made are as follows **
ie.navigate "https://www.flipkart.com/"
For Each Link In ElementCol
If Link.innerHTML = "Log In" _
And Link.getElementsByClassName = "_2k0gmP" _
Then
Link.Click
End If
Next Link

Try this code, worked for me to click on Log In
Code
#If VBA7 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Sub test()
Dim IE As Object
Dim htmlDoc As Object
Dim sURL As String
' CREATING OBJECT
Set IE = CreateObject("internetexplorer.application")
sURL = "https://www.flipkart.com/"
' WEBPAGE NAVIGATION
With IE
.navigate (sURL)
.Visible = True
End With
WaitIE IE, 2000
' CLICK ON LOG IN
Set htmlDoc = IE.document
Set buttonclick = htmlDoc.getElementsByClassName("_2k0gmP")
buttonclick(7).Click
WaitIE IE, 5000
'IE.Quit
'Set IE = Nothing
End Sub
Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
Sleep time
Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.readyState = 4) & _
vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
i = i + 1
Loop Until IE.readyState = 4 Or Not IE.Busy
End Sub
Edit
You can use this code to get the href:
For Each Element In buttonclick
Debug.Print Element.href
Next
Or to get the InnerText:
For Each Element In buttonclick
Debug.Print Element.innerText & " " & i
i = i + 1
Next
'Where i is the number of the item
Then you can use conditional such as:
For Each Element In buttonclick
If Element.href = "href_link" Then Element.Click
Next
OR
For Each Element In buttonclick
If Element.innerText = "Log In" Then Element.Click
Next

Related

VBA Click Button

Using VBA, how can I click the orange LOGIN button on the below page:
https://www.solaraccreditation.com.au/login.html
Inspect element shows:
<input class="button bg-orange" value="Login" type="submit">
So far my code is:
Option Explicit
Private Declare Sub ShowWindow Lib "user32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long)
Const strTMP As String = "Excel VBA"
Private Const SW_MAXIMIZE = 3
Public Sub Test()
Dim objWindow As Object
Dim objIEApp As Object
Dim objShell As Object
Dim objItem As Object
Dim objInputs As Object
Dim ele As Object
On Error GoTo Fin
Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
If LCase(objItem.FullName Like "*iexplore*") Then
Set objIEApp = objItem
End If
Next objItem
If objIEApp Is Nothing Then
Set objIEApp = CreateObject("InternetExplorer.Application")
objIEApp.Visible = True
End If
With objIEApp
.Visible = True
ShowWindow .hWnd, SW_MAXIMIZE
.Navigate "https://www.solaraccreditation.com.au/login.html"
While Not .ReadyState = 4
DoEvents
Wend
.document.All("username").Value = _
ThisWorkbook.Sheets("sheet1").Range("a1")
.document.All("password").Value = _
ThisWorkbook.Sheets("sheet1").Range("a2")
End With
' <input class="button bg-orange" type="submit" value="Login" />
Fin:
If Err.Number <> 0 Then MsgBox "Error: " & _
Err.Number & " " & Err.Description
Set objWindow = Nothing
Set objShell = Nothing
End Sub
Mary Poppins "Classic Disney" soundtrack. ... Um diddle diddle, diddle um, diddle ay x2 ? Now, you can say it backwards, which is docious-ali-expi-listic-frag
i-cali-rupus.
You could try identifying the button as the first in its class.
.document.getelementsbyclass("button bg-orange")(0).click
But it might be more expedient to simply force the form to submit.
.document.getelementbyid("loginForm").submit

How to get first 50 post url from instagram

I want to get the first 50 post for any public instagram url. To get the first 12 posts which are visible by default is easy and below code is doing that just fine but I am not sure how to access other urls which is visible when you hit "load more" button at the bottom and scroll further down for more. Is this possible? Can anyone help me with this?
Sub getData()
Dim urL As String, instaID As String, totalPost As Long, fRow As Long
Dim ie, var1, var2, a
urL = "https://www.instagram.com/nike/"
OUT.Range("A2:A" & OUT.Rows.Count).Clear
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
fRow = 2
'----------------------
With OUT
ie.navigate urL
'Busy
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set var1 = ie.document.getElementsByclassname("_mck9w _gvoze _f2mse")
Do While IsObject(var1) = False
Application.Wait Now + TimeValue("00:00:03")
Set var1 = ie.document.getElementsByclassname("_mck9w _gvoze _f2mse")
Loop
Set var2 = var1(0).document.getElementsBytagname("a")
For Each a In var2
If Trim(a.href) <> "" And a.parentelement.classname = "_mck9w _gvoze _f2mse" Then
.Range("A" & fRow) = a.href
Debug.Print a.innertext & " " & a.parentelement.classname
fRow = fRow + 1
End If
Next a
End With
'------------------
ie.Quit
Set ie = Nothing
Application.ScreenUpdating = True
End Sub
try this
Sub getData()
Dim urL As String, instaID As String, totalPost As Long, fRow As Long
Dim ie As Object, var1 As Object, a As Variant
urL = "https://www.instagram.com/nike/"
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate urL
Do While ie.Busy Or ie.readystate <> 4
DoEvents
Loop
Do
Set var1 = ie.Document.getElementsByclassname("_1cr2e _epyes") ' wait for "load more"
Loop While var1 Is Nothing
var1(0).Click ' click "load more"
Do While ie.Busy Or ie.readystate <> 4 ' not sure if this is needed
DoEvents
Loop
For a = 1 To 4
ie.Document.parentWindow.scrollBy 0, 60000 ' roll to bottom
Application.Wait Now + TimeValue("00:00:01") ' wait for "fill in"
Next a
Set var1 = ie.Document.getElementsBytagname("a")
Range("A2:A" & Rows.count).Clear
fRow = 2
For Each a In var1
If Trim(a.href) <> "" And a.parentelement.classname = "_mck9w _gvoze _f2mse" Then
Range("A" & fRow) = a.href
Debug.Print a.innertext & " " & a.parentelement.classname
fRow = fRow + 1
End If
Next a
' ------------------
ie.Quit
Set ie = Nothing
End Sub

How to fetch data inside <b> tag while web scraping in VBA

I am trying to scrape data from this website. i have written the code which opens this website and then searches for a value. clicks on the search result and opens the final page from where i have to pick the details. i need to pick details of tag mentioned in red in
this is my code which opens the desired page. I have used Link.click to open the desired page. After that i need to fetch details mentioned in image. Kindly advise.
Sub hullByAshish()
Dim html, html1 As HTMLDocument
Dim ElementCol, ElementCol1 As Object
Dim Link As Object
Dim appIE As Object
Dim a As String
Dim i As Long
Dim objElement As Object
Dim objCollection As Object
Set appIE = CreateObject("internetexplorer.application")
a = "PONTOVREMON"
With appIE
.Navigate "https://www.marinetraffic.com/en/ais/index/search/all/keyword:" & a
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Application.Wait (Now() + TimeValue("00:00:01"))
Set html = appIE.document
Set ElementCol = html.getElementsByTagName("a")
DoEvents
For Each Link In ElementCol
If Link.innerHTML = "PONTOVREMON" Then
Link.Click
End If
Next Link
End Sub
Here is one way
Dim ie As Object, ieDoc As Object, lnk As Object
Sub hullByAshish()
Dim IMO As String, MMSI As String, GTon As String
Set ie = CreateObject("internetexplorer.application")
a = "PONTOVREMON"
With ie
.Navigate "https://www.marinetraffic.com/en/ais/index/search/all/keyword:" & a
.Visible = True
End With
Do While ie.readystate <> 4: Wait 5: Loop
DoEvents
Set ieDoc = ie.document
For Each lnk In ieDoc.getElementsByTagName("a")
If lnk.innerhtml = "PONTOVREMON" Then
lnk.Click
Exit For
End If
Next lnk
Do While ie.readystate <> 4: Wait 5: Loop
IMO = GetValue("IMO:")
MMSI = GetValue("MMSI:")
GTon = GetValue("Gross Tonnage:")
Debug.Print "IMO: " & IMO
Debug.Print "MMSI: " & MMSI
Debug.Print "Gross Tonnage: " & GTon
End Sub
Function GetValue(s As String) As String
GetValue = Trim(Split(Split(Split(Trim(Split(ie.document.body.innerhtml, s)(1)))(0), "<b>")(1), "</b>")(0))
End Function
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
Screenshot

Web Scraper won't fill in a child window that my VBA code launches

I have the following code in VBA which opens up an IE page, fills it in and then clicks on a button that opens up a new IE window. However, my code is not able to fill in the first dropdown of the new window. Any help would be greatly appreciated.
Sub Van()
Dim IE As Object
Dim IE2 As Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE2 = CreateObject("InternetExplorer.Application")
IE.navigate ("website")
IE.Visible = True
Do
DoEvents
Loop Until IE.readyState = 4
Set d = IE.document
'Code now clicks on buttons or dropdowns etc
Application.Wait (Now + TimeValue("00:00:03"))
Set IE2 = GetIE("pop-up page")
WaitFor IE2
'Tried this too
'Application.Wait (Now + TimeValue("00:00:05"))
IE2.document.getElementsByTagName("select")(0).Value = "X"
'Also tried the line below
'IE2.document.GetElementsbyname("name")(0).SelectedIndex = 1
End Sub
Function GetIE(sAddress As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim retVal As Object, sURL As String
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
'see if IE is already open
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.LocationURL
On Error GoTo 0
If sURL <> "" Then
'MsgBox sURL
If sURL = sAddress Then
Set retVal = o
Exit For
End If
End If
Next o
Set GetIE = retVal
End Function
Sub WaitFor(IE)
Do
DoEvents
Loop Until IE.readyState = 4
End Sub
The page in the pop-up may not be fully loaded when you're trying to access the select - try adding a wait loop until loading is done. I like to pull out the "wait" loop into a re-usable function:
Set IE2 = GetIE("https://secure.apia.com.au/NASApp/apia/CRQuoteServlet?" & _
"pageAction=openModelSelectionWindow&currentSequenceNumber=")
WaitFor IE2
IE2.document.getElementsByTagName("select")(0).Value = "JAY"
WaitFor:
Sub WaitFor(IE)
Do
DoEvents
Loop Until IE.readystate = 4
End sub

Make Web Scraper manipulate a pop-up page that it opens from landing page

My code opens a page and starts to complete it. It then clicks on a button which results in a pop-up screen that needs to be completed. However, I'm not sure how to make my code access that pop up screen. Any help would be appreciated!
Here is my code:
Sub Van()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate ("website")
IE.Visible = True
Do
DoEvents
Loop Until IE.readystate = 4
Set d = IE.document
'Code clicks on buttons and dropdowns
Application.Wait (Now + TimeValue("00:00:03"))
d.GetElementbyid("caravanMake").Value = "JAY"
End Sub
This worked for me to set the value of the first drop-down in the pop-up:
'...
Application.Wait (Now + TimeValue("00:00:03"))
Set IE2 = GetIE("https://secure.apia.com.au/NASApp/apia/CRQuoteServlet?" & _
"pageAction=openModelSelectionWindow&currentSequenceNumber=")
IE2.document.getElementsByTagName("select")(0).Value = "JAY"
'etc
Function to find an open window with a given URL:
'Find an IE window with a matching URL
'Assumes no frames.
Function GetIE(sAddress As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim retVal As Object, sURL As String
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
'see if IE is already open
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.LocationURL
On Error GoTo 0
If sURL <> "" Then
'Debug.Print sURL
If sURL = sAddress Then
Set retVal = o
Exit For
End If
End If
Next o
Set GetIE = retVal
End Function