getting a number from a website - vba

I´m trying to get the number part of an Euro amount string from a website. I tried different thing things, that normally work. here are my 3 best attempts. nothing worked
My first attempt:
Sub gsellor()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
Dim HTMLPreisTag As IHTMLElement
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set HTMLPreisTag = objIE.document.getElementById("page_content").Children(0).Children(1).Children(0).Children(1).Children(0)
Debug.Print HTMLPreisTag.className
Debug.Print HTMLPreisTag.Children(1).Children(0).Children(0).innerText ' gets stuck right here
objIE.Quit
End Sub
the immediate window shows:
MarketInfo_market-info_3lkUj
My second attempt:
Sub gsellor()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
Dim HTMLPreisTag As IHTMLElement
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set HTMLtags = objIE.document.getElementsByTagName("span")
Debug.Print HTMLtags.Length
For Each HTMLtag In HTMLtags
Debug.Print "name: " & HTMLtag.className
Debug.Print "text: " & HTMLtag.innerText
Next HTMLtag
objIE.Quit
End Sub
the immediate window shows:
3
name:
text:
name: AccountPanel_account-panel-user-name_2TNIL
text:
name: sr-only
text: Toggle navigation
My third attempt:
Sub gsellor()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
Dim HTMLPreisTag As IHTMLElement
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set HTMLtags = objIE.document.getElementsByClassName("MarketInfo_market-num_1lAXs")
Debug.Print HTMLtags.Length
For Each HTMLtag In HTMLtags
Debug.Print "name: " & HTMLtag.className
Debug.Print "text: " & HTMLtag.innerText
Next HTMLtag
objIE.Quit
End Sub
immediate window:
0

To get the content you need to make your browser wait until the content is not nothing. This is another way to serve the same purpose:
Sub gsellor()
Dim ObjIE As New InternetExplorer
Dim Ohtml As HTMLDocument
Dim item_price As Object
With ObjIE
.Visible = True
.navigate "https://www.gdax.com/trade/LTC-EUR"
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set Ohtml = .document
End With
Do
Set item_price = Ohtml.getElementsByClassName("MarketInfo_market-num_1lAXs")(0)
DoEvents
Loop While item_price Is Nothing
MsgBox item_price.innerText
ObjIE.Quit
End Sub

Related

Type Mismatch on one machine

I wrote some code to scrape data from a website. I've tested it on 5 difference machines with different versions of excel and it all works fine. But on the intended users machine we get type mismatch error.The code fails at the last line below.
Sub LogIn()
Dim ie As SHDocVw.InternetExplorer
Dim iDoc As MSHTML.HTMLDocument
Dim ele As MSHTML.IHTMLElement
Dim eles As MSHTML.IHTMLElementCollection
Dim tableSection As MSHTML.IHTMLElement
Dim tableRow As MSHTML.IHTMLElement
Dim tableCell As MSHTML.IHTMLElement
Dim smallCell As MSHTML.IHTMLElement
Dim iCol As Integer
Dim iRow As Integer
Dim iCounter As Integer
iRow = 0
Do
iRow = iRow + 1
Loop Until Cells(iRow, 5) = ""
Range(Cells(1, 5), Cells(iRow, 6)).ClearContents
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate ("https://www.howdidido.com/")
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set iDoc = ie.document
any help greatly appreciated.
I have tried the following code and it is working alright. Maybe it can help you (seems as two loops and doEvents are needed for the ready state completes).
Dim iDoc As MSHTML.HTMLDocument
Dim iCol As Integer
Dim iRow As Integer
Dim iCounter As Integer
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://www.automateexcel.com/excel/"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
Set iDoc = IE.Document
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing

The data in A "id" is not printing

i am trying this
Sub JJ()
Dim IE As New SHDocVw.InternetExplorer
Dim hdoc As MSHTML.HTMLDocument
Dim ha As String
IE.Visible = True
IE.navigate "https://www.nseindia.com/get-quotes/equity?symbol=DIVISLAB"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set hdoc = IE.document
ha = hdoc.getElementById("preOpenFp").innerText
Debug.Print ha
End Sub
Please suggest any solution for that.
the point is in image.
If you want to read the data from the Pre-Open Markt you have to change the url as follows https://www.nseindia.com/get-quotes/equity?symbol=DIVISLAB#info-preopenmkt.Try this code.
Sub JJ()
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
'Dim IE As New SHDocVw.InternetExplorer
Dim hdoc As MSHTML.HTMLDocument
Dim ha As String
IE.Visible = True
IE.navigate "https://www.nseindia.com/get-quotes/equity?symbol=DIVISLAB#info-preopenmkt"
Do While IE.Busy: DoEvents: Loop
Do While IE.Busy And Not IE.readyState = READYSTATE_COMPLETE: DoEvents: Loop
'Application.Wait (Now + TimeValue("0:00:15"))
Set hdoc = IE.document
ha = hdoc.getElementById("preOpenIep").innerText
Debug.Print (ha)
End Sub

'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

Clicking a button in Internet Explorer

I am trying to create some VBA code that clicks a button with the following HTML:
<button class="button small btnViewAction atlas--ui-button" type="button" data-tracking-value="Action" data-tracking-label="View Action" data-tracking-## Heading ##category="Reconciliations" data-attribs-childassignmentid="661" data-attribs-reconciliationid="145870" data-attribs-assignmenttype="A" data-attribs-assignmentid="1223" value="undefined" data-columnname="action">Edit</button>
Is there a way to reference this button?
Without seeing more of the HTML I can't say this will work for sure, but it should give you a good guideline for what you can do!
Make sure you update ie.navigate to your site.
This is a late binding example, you can also set a reference to Microsoft Internet Controls.
Sub clickButton()
Dim ie As Object
Dim Btn As Object
'Late Binding
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo Catch
ie.Visible = True
ie.navigate "https://yourwebsite.com/"
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
'LOOP EACH CLASS ELEMENT
For Each Btn In ie.Document.getElementsByClassName("button")
If Btn.innertext = "Edit" Then
Btn.Click
End If
Next Btn
'CLOSE INSTANCE OF IE
Catch:
ie.Quit
Set ie = Nothing
End Sub
Here is a very generic sample of how to do this.
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
Try the code above and see if you can implement it for your specific situation. If you need a nudge, post back, with additional requirements.

Clicking on a link without Id or class

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