VBA: Go to IE site and tick by element - excel

New to navigating with IE. I m trying to go to a site https://www.thetrainline.com/ and tick the Return button by using element getElementsByID("return").FirstChild.Click However all this does is bring up the site. It doesn't tick the return option.
Any ideas?
Public Function GetHTML() As String
On Error GoTo ErrorEscape
Dim URL As String
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim HTML As Object
Dim myElement As Object
'Define URL
URL = "https://www.thetrainline.com/"
IE.navigate URL
Application.Wait (Now + TimeValue("0:00:03"))
MsgBox ("IE Loaded")
Set myElement = IE.getElementsByID("return").FirstChild.Click
Exit Function
ErrorEscape:
Set IE = Nothing
Set HTML = Nothing
End Function
P.S. I want many users to be able to use this without having to download something. Hence, why I am not using Selenium.

I try to modify your code to click on 'return' radio button.
Modified code:
Sub demo()
Dim URL As String
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim HTML As Object
Dim myElement As Object
'Define URL
URL = "https://www.thetrainline.com/"
IE.Visible = True
IE.navigate URL
Application.Wait (Now + TimeValue("0:00:03"))
'MsgBox ("IE Loaded")
IE.document.getElementById("return").Click
ErrorEscape:
Set IE = Nothing
Set HTML = Nothing
End Sub
Output:

Related

How to close a web message from IE VBA?

I,ve been dealing with a issue during my web scraping.
My problem is that when I click to "Submit" a form, it pops-up a web message that I've not been able to close.
I´ve already try the sendKeys Method but no success.
here is the link of the web page:
https://www3.bcb.gov.br/CALCIDADAO/publico/corrigirPorIndice.do?method=corrigirPorIndice
Here is a print of the info to put in the webpage
Here is a print of the info to put in the webpage
The message appers when you click the button on the left "Corrigir Valor"
enter image description here
the messege
enter image description here
obs. pressing enter or esc with the keyboard close it, but I`m not been able to do it in the code
here is the code if may be helpful
Sub atualizacao_valores()
Dim data_base As String
Dim data_atualizacao As String
Dim valor_face As String
Dim drp As HTMLFormElement
Dim html As HTMLDocument
Set ie = CreateObject("internetexplorer.application")
ie.navigate "https://www3.bcb.gov.br/CALCIDADAO/publico/exibirFormCorrecaoValores.do?method=exibirFormCorrecaoValores&aba=1"
ie.Visible = True
Do While ie.busy And ie.readyState <> "READYSTATE_COMPLETE"
DoEvents
Loop
data_base_ipcae = "01/2022"
data_atualizacao_ipcae = "12/2022"
valor_face = "100000"
Application.Wait (Now + TimeValue("00:00:02"))
Set html = ie.document
Set drp = html.getElementById("selIndice")
drp.selectedIndex = 4
ie.document.getelementsbytagname("input")(1).Value = data_base_ipcae
ie.document.getelementsbytagname("input")(2).Value = data_atualizacao_ipcae
ie.document.getelementsbytagname("input")(3).Value = valor_face
ie.document.getelementsbyclassname("botao")(0).Click
`here happens the pop up that I cant close
...
end sub
I agree with the suggestion given by Tim Williams.
Adding code below just above the line when you click the button could suppress the Alert().
With html.parentWindow
.execScript "window.alert = function(){return true;};", "JScript"
End With
Your modified code:
Sub atualizacao_valores()
Dim data_base As String
Dim data_atualizacao As String
Dim valor_face As String
Dim drp As HTMLFormElement
Dim html As HTMLDocument
Set ie = CreateObject("internetexplorer.application")
ie.navigate "https://www3.bcb.gov.br/CALCIDADAO/publico/exibirFormCorrecaoValores.do?method=exibirFormCorrecaoValores&aba=1"
ie.Visible = True
Do While ie.busy And ie.readyState <> "READYSTATE_COMPLETE"
DoEvents
Loop
data_base_ipcae = "01/2022"
data_atualizacao_ipcae = "12/2022"
valor_face = "100000"
Application.Wait (Now + TimeValue("00:00:02"))
Set html = ie.document
Set drp = html.getElementById("selIndice")
drp.selectedIndex = 4
ie.document.getelementsbytagname("input")(1).Value = data_base_ipcae
ie.document.getelementsbytagname("input")(2).Value = data_atualizacao_ipcae
ie.document.getelementsbytagname("input")(3).Value = valor_face
With html.parentWindow
.execScript "window.alert = function(){return true;};", "JScript"
End With
ie.document.getelementsbyclassname("botao")(0).Click
End Sub
I have tested this code on my end and it is suppressing the Alert message.
You could test it and let us know your test results.

Click on Class names in VBA - No Selenium

I am into Project Management with no knowledge of IT / Coding.
I am trying to do web automation.
It will have to click on certain links, submit a form and then hit save.
However I am not able to click on the first link as well.
this is my code as of now.
Sub CommandButton1_Click()
Dim ie As Object
Dim html As HTMLDocument
Dim form As Variant, button As Variant
Sheet1.Range("B6").Value = Application.UserName
'Open Internet Explorer
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://www.link.com/"
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
html.getElementsByClassID("p2205").Click
End Sub
Below is the code.
I want to click on Company Name.
Website Code
I have tried the web and YouTube videos as well but I guess I don't know the language or the logic behind coding. Hence I am not able to get through.
Add reference to Microsoft Internet Controls then try
Option Explicit
Sub CommandButton1_Click()
Dim ie As Object, html As HTMLDocument, e As HTMLHtmlElement
Dim URL As String
URL = "https://www.link.com/"
Set ie = New InternetExplorerMedium
ie.navigate URL
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
For Each e In html.getElementsByClassName("dsh_sta_301")
If InStr(e.onclick, "projectSelection") > 0 Then
e.Click
End If
Next
End Sub

Soundcloud song names

I'm new to VBA web scraping and html structure in general and I'm having some trouble getting the song names off this html address "https://soundcloud.com/maraudamusic/tracks"
This is what I have tried so far and I can't seem to pull the <li> song names from the html document (I barely know what I'm talking about so forgive me).
Option Explicit
Sub SCScrape()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim site As String
Dim artist As String
artist = "maraudamusic"
site = "https://soundcloud.com/" & artist & "/tracks"
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate site
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = New HTMLDocument
Dim ul As IHTMLElementCollection
'Dim ul As Variant
'Dim li As IHTMLListElement
Dim els As Variant
Dim el As Variant
'Dim li As Variant
'Set ul = html.getElementsByClassName("lazyLoadingList__list sc-list-nostyle sc-clearfix")
'Set ul = html.getElementsByTagName("ul")
Set els = html.getElementsByClassName("soundTitle_usernameTitleContainer")
Debug.Print
For Each el In els
Debug.Print el.innerHTML
Next el
Debug.Print "Hello"
End Sub
The main issue is trying to read elements from a blank document...
You set html = New HTMLDocument. This instantiates the html object as a blank HTMLDocument object.
What you should do is set html = ie.document to read the contents of the current document in the IE window.
Also, as Soundcloud is a Javascript/AJAX type application, you may need to wait a couple of seconds for AJAX calls to finish. This may not be necessary but I find it sometimes helps.
Here's a working example.
Option Explicit
Sub SCScrape()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim site As String
Dim artist As String
artist = "maraudamusic"
site = "https://soundcloud.com/" & artist & "/tracks"
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate site
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:03")) ' wait for ajax...
Set html = ie.Document 'get the document from IE
Dim els As Variant
Dim el As Variant
Set els = html.getElementsByClassName("soundTitle__title")
For Each el In els
Debug.Print el.innerText
Next el
Debug.Print "Hello"
End Sub

Excel VBA : How to copy href from HTML by referencing the class name

<span class="export excel">Excel </span>|
How can I copy the href in this HTML via Excel VBA?
This is my code, but it doesn't work.
Set Export = ie.Document.all("export excel")
URL = Export.href
ie.Navigate URL
This code you should walk through in debug mode ... it's more for instructional use than for production, but it will do what you want from it. I used Google as a test site.
Sub Test()
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim Link As String, Target As Object
Link = "http://www.google.com"
' start browser
Set Browser = New SHDocVw.InternetExplorer
Browser.Visible = True
' wait a bit
Browser.Navigate Link
' wait a bit
Set HTMLDoc = Browser.Document
' wait a bit
Set Target = GetElementByTagAndClassName(HTMLDoc, "SPAN", "gbtb2")
If Not (Target Is Nothing) Then
' test here if parent really is a <a>
Debug.Print Target.parentElement.href
' ta-taaaa!!!
End If
End Sub
' get element by tag and attribute value
Function GetElementByTagAndClassName(Doc As MSHTML.HTMLDocument, ByVal Tag As String, ByVal Match As String) As MSHTML.IHTMLElement
Dim ECol As MSHTML.IHTMLElementCollection
Dim IFld As MSHTML.IHTMLElement
Set GetElementByTagAndClassName = Nothing
Set ECol = Doc.getElementsByTagName(Tag)
For Each IFld In ECol
' Debug.Print IFld.className
If IFld.className = Match Then
Set GetElementByTagAndClassName = IFld
Exit Function
End If
Next
End Function

excel vba IE clicking a button

Through Excel and VBA, i am opening a page.
I want to click on the button 'Expand' through vba but I am getting an error.
I tried using both VBA commands listed below
Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")(0).Click
Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all").Click
The error that I get is
Run time error '438': Object doesn't support this property or method.
Assuming that "Doc" is your reference to IE try this:
Set ElementCol = Doc.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")
For Each btnInput In ElementCol
btnInput.Click
Next btnInput
Working Example:
Private Sub IE_Expand()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'IE.Visible = False
IE.Navigate "https://dhli.dhl.com/dhli-client/shipmentair;jsessionid=q3tzTzyLcL7JkxkNQ4nv7Jtrpzk1glylCyJ7vJzT27h2xBG5zXSm!599496067?0&shipmentId=151218573&accountGroup"
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.Visible = True
Set ElementCol = IE.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")
ElementCol.Item(0).Click
' Clean up
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
Application.StatusBar = ""
End Sub

Resources