Use VBA to find submenu element and click - html

I am fairly new to vba and am self taught but have gotten my vba to log in to my website and get to the homepage. Next I need to click on a submenu item but I am struggling to find it. When manually doing this, the submenu works when I hover over the icon and the click on a button called "Comed Reports" below which I believe is the element ID "Report1017".
Below is the html code from the website:
And below is where my code is at this stage:
Sub Login()
Const Url$ = "examplewebsite.com"
Dim HTMLDoc As HTMLDocument
Dim oHTML_Element As IHTMLElement
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Navigate Url
.Visible = True
Do While ie.Busy Or ie.ReadyState < 4
DoEvents
Loop
Set HTMLDoc = ie.Document
Dim Login As Object
Dim Password As Object
Dim LoginButton As Object
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Name Like "txt_1*" Then Set Login = oHTML_Element
If oHTML_Element.Name Like "txt_2*" Then Set Password = oHTML_Element
If oHTML_Element.Name Like "btnLogin*" Then Set LoginButton = oHTML_Element
Next
Login.Value = ""
Password.Value = ""
LoginButton.Click
Do While ie.Busy Or ie.ReadyState < 4
DoEvents
Loop
' Find Submenu Report1017 here
End With
End Sub

If it is always Report1017 you probably just need to run the javascript:
HTMLDoc.parentWindow.execScript "LoadContent('Report1017','ReportList.aspx?ReportParam=1017')", "JavaScript"
Give it a try.

Related

Is there a way to use VBA to click on a button on a website?

I am trying to click on a button labeled "CSV" on this website but it is giving me trouble. I can get VBA to open the website but not actually click the button. Any suggestions on the code would be greatly appreciated.
Sub website_test()
Dim ie As Object
Dim ht As HTMLDocument
Dim button As Object
Dim i As Integer
For i = 0 To 5
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("https://www.dailyfantasyfuel.com/nfl/projections/fanduel/2022-09-11/")
Do Until ie.readyState = 4
DoEvents
Loop
Set ht = ie.document
Set elems = ht.getElementsByClassName("title may-blank")
For Each elem In elems
Debug.Print (elem.innerText)
Next
Set button = ht.querySelector("span class=hov-underline")
button.Click
ie.Quit
Next i
End Sub

How is it possible to click on a button on website using VBA?

I have a button that is present on a website that I want to click using VBA code.
. This is what the HTML code of the button looks like.
I wrote this piece of code to click the button
Set the_input_elements = objIE.document.getElementsByTagName("a")
For Each input_element In the_input_elements
If input_element.className = "big_button" _
And input_element.href = "javascript:changePageToFrontdoor(false);" Then
input_element.Click
Exit For
End If
Next
However, this is not clicking the button. No error message. Just nothing happening with the page.
Can anyone tell me where is my error?
Thank you :)
Tri something like this
Sub Login()
Dim IeApp As InternetExplorer
Dim IeDoc As Object
Dim ieTable As Object
Set IeApp = New InternetExplorer
IeApp.Visible = True
IeApp.Navigate "http://yourlink.com"
Do While IeApp.Busy: DoEvents: Loop
Do Until IeApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc = IeApp.document
Set the_input_elements = IeDoc.getElementsByClassName("big_button")
For Each input_element In the_input_elements
If input_element.href = "javascript:changePageToFrontdoor(false);" Then
input_element.Click
Exit For
End If
Next input_element
End Sub

Excel vba for automating website

I'm a beginner in excel vba.
I'm trying to click html element by id but it is not working
This is my code for my website...
I navigated to website and put user ID and password. Thats ok,just like i mentioned in code they all working but after i logged in (i attached an imaged link),when i inspect the html that i want to click, it shows like this...
HTML Element Codes
Sub WebAuto()
Dim objIE As New InternetExplorer
Dim oHTML_Element As IHTMLElement
Dim inputCollection As HTMLElementCollection
Dim i As HTMLInputElement
Dim obj As Object
objIE.Visible = True
objIE.Navigate ("***") 'for example
Do
DoEvents
Loop Until objIE.ReadyState = 4
objIE.Document.getElementById("uid").Value = "***" 'user ID typed
objIE.Document.getElementById("password").Value = "***" 'pw typed
With objIE.Document
Set obj = .getElementsByTagName("input")
'Debug.Print TypeName(aeiou)
For Each i In obj
If (i.getAttribute("value") = "Log In") Then
i.Click
Debug.Print "Logged In"
Do
DoEvents
Loop Until objIE.ReadyState = 4
End If
Next i
End With
End Sub
HTML Element to click
This is my very first question on Stack Overflow, so please forgive me if i made any mistakes. Thank you all..

Excel VBA code to click web button

Need help how to create excel vba code for this
I'll be needing the codes so I can complete my macro.
Thanks in advance
First, you will need to create a reference to:
Microsoft Internet Controls
Microsoft HTML Object Library
In VBE, click Tools > References
Sub clickLink()
Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
Url = "http://UrlToYourLink.com"
With ie
.navigate Url
Do While .Busy Or .readyState < READYSTATE_COMPLETE
DoEvents
Loop
doc = .document
.Visible = True
End With
Dim myBtn As Object
Set myBtn = doc.getElementsByClassName("button rounded")(0)
myBtn.Click
End Sub
The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
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
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
Microsoft internet controls are a great way to do this, but if you aren't allowed to add new references, here is another way to go about web scraping.
This methode ain't as 'clean' as Microsoft internet controls and HTML object but it gets the job done.
Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer
'initialize counter
i = 0
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'navigate to the url
ie.navigate "Www.google.com"
'Statusbar shows in the buttom corner of excel
Application.StatusBar = "Loading, please wait..."
'Wait until page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Store all the elements with input tag
Set objCollection = ie.Document.getElementsByTagName("input")
'Go through all input elements
While i < objCollection.Length
'input search field
If objCollection(i).Name = "q" Then
objCollection(i).Value = "Hello World"
End If
'search button
If objCollection(i).Type = "submit" Then
Set objSearchBnt = objCollection(i)
End If
i = i + 1
Wend
objSearchBnt.Click
'Clean up
Set objSearchBnt = Nothing
Set objCollection = Nothing
Set ie = Nothing
'Give excel control over the status bar agian
Application.StatusBar = ""
End Sub

Pull value from website (HTML div class) using Excel VBA

I'm trying to automate going to a website and pulling the ratings from several apps.
I've figured out how to navigate and login to the page.
How do I pull the element - the number "3.3" in this case - from this specific section into Excel.
Being unfamiliar with HTML in VBA, I got this far following tutorials/other questions.
Rating on website and the code behind it
Sub PullRating()
Dim HTMLDoc As HTMLDocument
Dim ie As InternetExplorer
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.appannie.com/account/login/xxxxxxxxxx"
Set ie = New InternetExplorer
ie.Silent = True
ie.navigate sURL
ie.Visible = True
Do
'Wait until the Browser is loaded
Loop Until ie.readyState = READYSTATE_COMPLETE
Set HTMLDoc = ie.Document
HTMLDoc.all.Email.Value = "xxxxxxxxx#xxx.com"
HTMLDoc.all.Password.Value = "xxxxx"
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Dim rating As Variant
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
Range("A1").Value = rating
'ie.Refresh 'Refresh if required
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
The code below will let you extract text from first element with class name "rating-number ng-binding" in HTML document. By the way GetElementsByClassName is supported since IE 9.0. I use coding compatible also with older versions in my example.
Dim htmlEle1 as IHTMLElement
For Each htmlEle1 in HTMLDoc.getElementsByTagName("div")
If htmlEle1.className = "rating-number ng-binding" then
Range("A1").Value = htmlEle1.InnerText
Exit For
End if
Next htmlEle1
While Ryszards code should do the trick if you want to use the code you have already written then here is the alterations I believe you need to make.
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
'Need to wait for page to load before collecting the value
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim rating As IHTMLElement
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
'Need to get the innerhtml of the element
Range("A1").Value = rating.innerhtml