Clicking on a link without Id or class - vba

I have a code that does a research for the user in a website and then get the information from that research.
In the process of acessing the information, I need to click o a link inside the website. This link can change everytime the user makes a different research, so I can't use it's href to acess it. I have no clue about what to do.
Here's my code until now: it goes until the webpage where the link is.
Steps of the code:
user insert his research. example: "icms base de calculo pis cofins"
open ie in: http://www.stj.jus.br/SCON/
insert the research
click on: "sumulas", "decisões monocromaticas" and "informativos de jurisprudencia". That way I acess only the "acordãos".
click on "pesquisar"
I need to click on the link in front of "Acórdãos"
Here's the code:
Sub teses2()
Dim pesquisa As String
Dim ie As InternetExplorer
Dim elemns As Object
Dim elem As Object
Dim elemns2 As Object
Dim elem2 As Object
Dim elem3 As Object
Dim obj As Object
pesquisa = InputBox("Digite os termos que quer pesquisar: ", "", "")
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "http://www.stj.jus.br/SCON/"
Application.Wait (Now + TimeSerial(0, 0, 20))
ie.document.getElementById("pesquisaLivre").innerText = pesquisa
Set elemns2 = ie.document.getElementsByTagName("input")
For Each elem2 In elemns2
If elem2.Value = "SUMU" Then
elem2.Click
End If
If elem2.Value = "DTXT" Then
elem2.Click
End If
If elem2.Value = "INFJ" Then
elem2.Click
End If
Next
Set elemns = ie.document.getElementsByTagName("input")
For Each elem In elemns
If elem.Value = "Pesquisar" Then
elem.Click
End If
Next
End Sub

I have revisited this and made it tighter.
Option Explicit
Sub teses2()
Dim pesquisa As String
Dim ie As InternetExplorer
'*SUSPENDED * pesquisa = InputBox("Digite os termos que quer pesquisar: ", "", "")
pesquisa = "icms base de calculo pis cofins"
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "http://www.stj.jus.br/SCON/"
While ie.Busy
DoEvents
Wend
While ie.Document.ReadyState = "complete"
DoEvents
Wend
'Stop
Application.Wait (Now + TimeSerial(0, 0, 3))
ie.Document.getElementById("pesquisaLivre").innerText = pesquisa
Dim oHtml As MSHTML.HTMLDocument '* gives intellisense
Set oHtml = ie.Document
Dim oSelectors As MSHTML.IHTMLDOMChildrenCollection
Set oSelectors = oHtml.querySelectorAll("div.blocoCampos input")
Dim lSelectorResultList As Long
lSelectorResultList = oSelectors.Length
Dim lSelectorResultLoop As Long
For lSelectorResultLoop = 0 To lSelectorResultList - 1
Dim objInputCheckbox As Object
Set objInputCheckbox = oSelectors.Item(lSelectorResultLoop)
If Not objInputCheckbox Is Nothing Then
Dim sLabel As String
sLabel = objInputCheckbox.Value
If VBA.InStr(1, "|SUMU|DTXT|INFJ|", "|" & sLabel & "|", vbTextCompare) > 0 Then
objInputCheckbox.Click
End If
End If
Next
'* release references
Set objInputCheckbox = Nothing
Set oSelectors = Nothing
Set oHtml = Nothing
Dim elemns As MSHTML.IHTMLDOMChildrenCollection
'Set elemns = ie.Document.getElementsByTagName("input")
Set oHtml = ie.Document
Set elemns = oHtml.querySelectorAll("div#botoesPesquisa input:nth-child(1)")
Debug.Assert elemns.Length = 1
Dim elem As Object
Set elem = elemns.Item(0)
'For Each elem In elemns
If elem.Value = "Pesquisar" Then
elem.Click
End If
'Next
'* release references
Set elem = Nothing
Set elemns = Nothing
While ie.Busy
DoEvents
Wend
'While ie.Document.ReadyState = "complete"
' DoEvents
'Wend
'* POST NAVIGATION
'Stop
Application.Wait (Now + TimeSerial(0, 0, 10))
Set oHtml = ie.Document
Dim objResultList As MSHTML.IHTMLDOMChildrenCollection
Set objResultList = oHtml.querySelectorAll("div#itemlistaresultados span:nth-child(2) a")
Dim lResultCount As Long
lResultCount = objResultList.Length
Debug.Print
Dim lResultLoop As Long
For lResultLoop = 0 To lResultCount - 1
Dim anchorLoop As MSHTML.HTMLAnchorElement
Set anchorLoop = objResultList.Item(lResultLoop)
Debug.Print anchorLoop.href
Next
ie.Quit
Set ie = Nothing
'Stop
End Sub

Related

'Busy' method of 'IUWebBrowser2' object failed

When I launch the site via my code, there is an error of type "method document of object iwebbrowser2 failed " at the level of my variable "oDoc"
Private Function CreerNavigateur(ByVal mails As String)
Dim IE As Object
Dim oDoc As Object
Dim Htable, maTable As Object
Dim text As String
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate "https://csrtool-ssl.sso.infra.ftgroup/csrtool_web/Bricks/pg/osuit/pages/identity/IdentityAccountAndUsers?type=emailAlias&value=" & mails & "&tab_main=AccountInfo"
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set oDoc = IE.Document
Set Htable = oDoc.getElementsByTagName("div")(1)
' MsgBox Htable.innerhtml
Set maTable = Htable.getElementsByTagName("span")
'MsgBox maTable(0).href
'myData = maTable(0).innertext
'MsgBox (myData)
IE.Quit
'On libère les variables
Set IE = Nothing
Set oDoc = Nothing
End Function
thank you for helping me to see my mistake
formalizing my answer try maximized ie window and updated code will be
Private Function CreerNavigateur(ByVal mails As String)
Dim ie As SHDocVw.InternetExplorer
Dim oDoc As Object
Dim Htable, maTable As Object
Dim text As String
Set ie = New SHDocVw.InternetExplorer
ie.Visible = False
ie.navigate "https://csrtool-ssl.sso.infra.ftgroup/csrtool_web/Bricks/pg/osuit/pages/identity/IdentityAccountAndUsers?type=emailAlias&value=" & mails & "&tab_main=AccountInfo"
While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set oDoc = ie.Document
Set Htable = oDoc.getElementsByTagName("div")(1)
'MsgBox Htable.innerhtml
Set maTable = Htable.getElementsByTagName("span")
'MsgBox maTable(0).href
'myData = maTable(0).innertext
'MsgBox (myData)
ie.Quit
'On libère les variables
Set ie = Nothing
Set oDoc = Nothing
End Function

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")

How to import data from multiple pages?

I have a small piece of code that lists links in a site.
Sub ListLinks()
'Set a reference to microsoft Internet Controls
Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim i As Long
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "http://www.sharenet.co.za/v3/q_sharelookup.php"
IeApp.Navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
Set IeDoc = IeApp.Document
For i = 0 To IeDoc.Links.Length - 1
Cells(i + 1, 1).Value = IeDoc.Links(i).href
Next i
Set IeApp = Nothing
End Sub
That is very useful for listing all links in a site. How can I loop through these URLs and import data from each one?
For instance, the first link under 'Name or Sector' is this:
http://www.sharenet.co.za/v3/sharesfound.php?ssector=0533&exch=JSE&bookmark=Oil & Gas&scheme=default
There is actually nothing to import from there. The next link has some data:
http://www.sharenet.co.za/v3/sharesfound.php?ssector=0537&exch=JSE&bookmark=Oil%20-%20Integrated&scheme=default
The data from there looks like this:
Name Full Name Code Sector
SACOIL-N Sacoil Holdings Ltd NPL SCLN 0537
ERIN Erin Energy Corporation ERN 0537
BEE-SASOL BEE - SASOL LIMITED SOLBE1 0537
SACOIL SACOIL HOLDINGS LD SCL 0537
OANDO OANDO PLC OAO 0537
OANDORIGT OANDO PLC RIGT OAON 0537
MONTAUK Montauk Holdings Ltd MNK 0537
How can I import that data from each link?
This seems to work pretty well. It may need a bit of fine tuning, but this should be pretty darn close.
Sub ListLinks()
'Set a reference to microsoft Internet Controls
Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim i As Long
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "http://www.sharenet.co.za/v3/q_sharelookup.php"
IeApp.Navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
Set IeDoc = IeApp.Document
For i = 0 To IeDoc.Links.Length - 1
Cells(i + 1, 1).Value = IeDoc.Links(i).href
Next i
Set IeApp = Nothing
Call CopyFromURL
End Sub
Public Sub CopyFromURL()
Dim IE As InternetExplorer, doc As HTMLDocument
Dim thisClass As IHTMLElement2, thisLink As IHTMLElement
Dim rng As Range, cell As Range
Const READYSTATE_COMPLETE As Integer = 4
Dim TR_col As Object, TR As Object
Dim TD_col As Object, TD As Object
Dim row As Long, col As Long
row = 1
Set rng = Range("A1:A5")
For Each cell In rng
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate cell
Do While (IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE)
DoEvents
Loop
Set TR_col = IE.Document.getElementsByTagName("TR")
For Each TR In TR_col
Set TD_col = TR.getElementsByTagName("TD")
col = 2
For Each TD In TD_col
Cells(row, col) = TD.innerText
col = col + 1
Next
col = 2
row = row + 1
Next
Next cell
IE.Quit
End Sub

VBA to Excel how do i get data name and phone numbers instead of hyperlinks?

Sub GetAllLinks()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
url_name = "http://www.trovanumeri.com/?azione=cerca&cerca=portoscuso"
If url_name = "" Then Exit Sub
IE.navigate url_name
Do
DoEvents
Loop Until IE.readyState = 4
'<a href="http://google.com">Click Here
Set AllHyperLinks = IE.document.getElementsByTagName("A")
Foglio1.ListBox1.Clear
'this is the code
For Each hyper_link In AllHyperLinks
Foglio1.ListBox1.AddItem hyper_link
Next
MsgBox "Done!"
End Sub
Take a look at the below example, showing how to retrieve the data from via IE automation and DOM processing:
Option Explicit
Sub GetData()
Dim oIE As Object
Dim lCurRow As Long
Dim lResultIndex As Long
Dim sUrl As String
Dim oTable0 As Object
Dim oTable1 As Object
Dim oTable2 As Object
Dim oTable3 As Object
Dim sTbl3Text As String
Dim cAncorNodes As Object
Dim oAncorNode As Variant
Dim sRowText As String
Dim oRowNode As Object
Dim aData() As Variant
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.Visible = True
Sheets(1).Cells.Delete
lCurRow = 1 ' Worksheet rows counter
lResultIndex = 0 ' Search result index counter
Do
' Navigate to the page
sUrl = "http://www.trovanumeri.com/?azione=cerca&cerca=cagliari&da=" & lResultIndex
.Navigate sUrl
' Wait IE
Do While .ReadyState < 3 Or .Busy
DoEvents
Loop
' Wait Document
Do Until .Document.ReadyState = "complete"
DoEvents
Loop
' Retrieve target tables
Set oTable0 = .Document.getElementsByTagName("table")(0)
Set oTable1 = oTable0.getElementsByTagName("table")(1)
Set oTable2 = oTable1.getElementsByTagName("table")(3)
' Get and process ancor nodes
Set cAncorNodes = oTable2.getElementsByTagName("a")
For Each oAncorNode In cAncorNodes
With CreateObject("Scripting.Dictionary")
' Add .href to result
.Add .Count, oAncorNode.href
' Get ancor's parent row
Set oRowNode = oAncorNode.ParentNode.ParentNode.ParentNode
Do
' Add nonemtpy row to result
sRowText = Trim(Replace(oRowNode.innerText, vbCrLf, ""))
If sRowText <> "" Then .Add .Count, sRowText
' If last row then exit
If IsNull(oRowNode.nextElementSibling) Then Exit Do
' Proceed with next row
Set oRowNode = oRowNode.nextElementSibling
' If net row contains oAncorNode then exit
If oRowNode.getElementsByTagName("a").Length > 0 Then Exit Do
DoEvents
Loop
' Get results as array
aData = .Items
End With
' Output array to worksheet row
With Sheets(1).Cells(lCurRow, 1)
.Resize(1, UBound(aData) + 1) = aData
.Select
End With
lCurRow = lCurRow + 1
DoEvents
Next
' Get table containing 'Next' button
Set oTable3 = oTable0.getElementsByTagName("table")(7)
sTbl3Text = oTable3.innerText
' If no 'Next' button then exit
If InStr(sTbl3Text, "Avanti >>") = 0 Then Exit Do
lResultIndex = lResultIndex + 10
DoEvents
Loop
.Quit
End With
End Sub

Scraping from VBA (very close to working!!)

I have been playing around with a scraper for consumer stocks and I can scrape data from the main page of items but once i start using the second, thid parges,
Sub asosdesc2()
Const READYSTATE_COMPLETE = 4
Dim j As Integer
Dim ie As InternetExplorer
Dim Doc As IHTMLDocument
Dim xcolElements As IHTMLElementCollection
Dim ell As IHTMLElement
Dim pn As Integer
j = 1
Set ie = CreateObject("InternetExplorer.Application")
For pn = 1 To 1
ie.Visible = False
ie.Navigate "http://www.asos.com/Men/Sale/Accessories/Cat/pgecategory.aspx?cid=2097&CTARef=shop|sale|cat|accessories#parentID=-1&pge=0&pgeSize=36&sort=-1"
Do
DoEvents
Loop Until Not ie.Busy And ie.readyState = READYSTATE_COMPLETE
Set Doc = ie.Document
'Set xcolElements = Doc.getElementsByClassName("description")
'Here lies the problem. Description works perflectly, price doesn't
Set xcolElements = Doc.getElementsByClassName("desc")
For Each ell In xcolElements
On Error GoTo Skip
Range("B" & j).Value = ell.innerText
j = j + 1
Skip:
Next ell
On Error GoTo 0
Next pn
ie.Quit
Set el = Nothing
Set xcolElements = Nothing
Set Doc = Nothing
Set ie = Nothing
End Sub
I am trying to return the description, the price and the HREF code to identify the item
Instead of looping through the collection of "desc" anchors (which only contain the product description), I suggest you loop through their parent "items" collection. That way you can reference your anchors' siblings, which contain price etc.:
Sub asosdesc2()
Const READYSTATE_COMPLETE = 4
Dim j As Integer
Dim ie As InternetExplorer
Dim Doc As IHTMLDocument
Dim itemsColl As IHTMLElementCollection
Dim itemsEle As IHTMLElement
Dim itemsAttributesColl As IHTMLElementCollection
Dim itemsAttributesEle As IHTMLElement
Dim pn As Integer
On Error GoTo 0
j = 1
Set ie = CreateObject("InternetExplorer.Application")
For pn = 1 To 1
ie.Visible = True
ie.Navigate "http://www.asos.com/Men/Sale/Accessories/Cat/pgecategory.aspx?cid=2097&CTARef=shop|sale|cat|accessories#parentID=-1&pge=0&pgeSize=36&sort=-1"
Do
DoEvents
Loop Until Not ie.Busy And ie.readyState = READYSTATE_COMPLETE
Set Doc = ie.Document
Set itemsColl = Doc.getElementById("items").Children
For Each itemsEle In itemsColl
On Error GoTo Skip
Set itemsAttributesColl = itemsEle.Children
For Each itemsAttributesEle In itemsAttributesColl
If itemsAttributesEle.className = "desc" Then
Range("B" & j).Value = itemsAttributesEle.innerText 'Description
Range("E" & j).Value = itemsAttributesEle.getAttribute("href") 'Link
End If
If itemsAttributesEle.className = "productprice" Then
Range("C" & j).Value = itemsAttributesEle.Children(0).innerText 'recRP
Range("D" & j).Value = itemsAttributesEle.Children(1).innerText 'Outlet current price
End If
Next itemsAttributesEle
j = j + 1
Skip:
Next itemsEle
On Error GoTo 0
Next pn
ie.Quit
Set itemsColl = Nothing
Set itemsEle = Nothing
Set itemsAttributesColl = Nothing
Set itemsAttributesEle = Nothing
Set Doc = Nothing
Set ie = Nothing
End Sub