Excel vba for automating website - html

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..

Related

Open a webpage with VBA and click on the "active directory" button to log in

I want to open a web page directly from Excel VBA where I need to click on the "active directory" button which has no ID. This button uses the Windows credentials to automatically log so I just need to click there to later once I'm logged I can start populating some fields.
This generic code is giving me an error message
" The object invoked has disconnected from its clients"
I assume it's related to the web page secure log on.
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub
IE is EOL and shouldn't be used anymore.
I can't tell you how exactly on the page the button is clicked, because that requires the HTML code. But the disconnection from the client may be due to the way IE is initialized. Try it via the GUID D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub

Use VBA to find submenu element and click

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.

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

How can I pull data from website using vba

I am new at vba coding to pull data from website so generally, I use this code to connect and check item to pull data from website but this code cannot check data via watch in vba with my firm webapp. it show nothing when I add watch to the class so what should I do.HTML Code from my firm webapp 1
HTML Code from my firm webapp 2
Sub Connect_web()
Dim ie As InternetExplorer
Dim doc As HTMLdocument
Dim ele As IHTMLElement
Dim col As IHTMLElementCollection
Dim ele_tmp As IHTMLElement
Set ie = New InternetExplorer
URL = "" ' Cannot provide
ie.Visible = True
ie.navigate URL
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading Page..."
DoEvents
End If
Loop
Set doc = ie.Document
Set ele = doc.getElementByClassName("GDB3EHGDHLC")
end sub
Let's start with four things:
1) Instead of .Navigate use .Navigate2
2) Use a proper wait
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
3) Correct the syntax of your Set ele line. You are using ByClassNamewhich returns a collection and therefore is plural. You are missing the s at the end of element.
As you have declared ele as singular (element), perhaps first set the collection into a separate variable and index into that collection.
Dim eles As Object, ele As Object
Set eles = doc.getElementsByClassName("GDB3EHGDHLC")
Set ele = eles(0)
4) You should always use id over other attributes, if possible, as id is usually quicker for retrieval. There is an id against that class name in your image (highlighted element). I am not going to try and type it all out. Please share your HTML using the snippet tool, by editing your question, so we can relate to your html in answer easily.
Set ele = doc.getElementById("gwt-debug-restOfIdStringGoesHere")

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