Using VBA to navigate a href in IE HTML document - html

I am trying to use VBA to navigate IE to search some of the information for me. However, after the code is written and I run it. I found everything is fine before the part of clicking href, but if I use "F8" to execute the code one by one and it works. Unfortunately, I haven't found some situation like mine in this forum. Can someone give some hints to me? Thanks
Sub GetData()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
Dim HTMLAllhref As MSHTML.IHTMLElementCollection
studentid = 12345678
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate "https://xxx.xxx"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("abcd")
HTMLInput.Focus
HTMLInput.FireEvent("onchange")
HTMLInput.Value = studentid
Set HTMLButton = HTMLDoc.getElementById("submit")
HTMLButton.Click
The part above works properly, but the following part works only if I use "F8" to execute one by one
Set HTMLAllhref = HTMLDoc.GetElementsByTagName ("a")
For Each link in HTMLAllhref
If InStr(link.innerText, studentid) Then
IE.Navigate link.href
Exit For
End If
Next
End Sub

1) If works with F8 it is usually a timing issue and you need to determine where a wait needs to happen.
As a minimum be sure to use a proper wait after each .Navigate2, .Click and .Submit to allow for page loading:
While IE.Busy Or ie.readyState < 4: DoEvents: Wend
Potentially also after:
HTMLInput.FireEvent("onchange")
2) The recommended method is .navigate2, not .navigate.

Related

Web Side Login / no debug.print shown in specific Window

I want to login a web side
After a lot of trial and errors I finally wrote a code that works pretty well.
.. but got probs
First I got a prob that occurs and said
no connection to server
This Prob I solved
Set ie = CreateObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
The next Prob is that when I ran the code Step by step (Trigger F8) the code runs pretty well.But Triggering F5 I got the Prob in Line
Set UserNameInput = LoginForm.getElementsByClassName("prePopulatedCredentials")(0)
This.. I do not understand
The next Prob is a debug.print "Prob"
Although I activated the Window (STR+G) debug.print is not shown.
Why want I see the debug Print result?
So.. I want to find out which Button I have to trigger by using the VBA Code
In my case I have 3 options
Search, new Search, go back or Sign In
For Sign In I found a solution for another web Side
Sub TestLogin()
Dim...
Dim LoginForm As MSHTML.HTMLFormElement
.. code
LoginForm.submit
For another Web Side
I want to find the buttons
Search, new Search, go back
The code is almost the same
Sub Get()
Dim...
Dim HTML Button AS MSHTML.IHTMLELEMENT
... Code
Set Buttons= HTMLDoc.GetElementsByTagName("button")
For Each HTMLButton In HTMLButtons
Debug.Print HTMLButton.className, HTMLButton.tagName, HTMLButton.Id, HTMLButton.innerText
Next Button
End Sub
In this case the code runs until the line
Set Buttons= HTMLDoc.GetElementsByTagName("button")
and it jumps over to End Sub
Here my Code for the first web side with the Prob Triggering the code with F8 or F5
Sub TestLogin()
Dim ie As SHDocVw.InternetExplorerMedium
Sub TestLogin()
Dim ie As SHDocVw.InternetExplorerMedium
Dim doc As MSHTML.HTMLDocument
Dim LoginForm As MSHTML.HTMLFormElement
Dim UserNameInput As MSHTML.HTMLInputElement
Dim PasswordInput As MSHTML.HTMLInputElement
Set ie = New SHDocVw.InternetExplorer
' Here I found a solution in the www regarding the Prob Net.Framework
Set ie = CreateObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
ie.Visible = True
ie.Navigate Sheet1.Range("B1").Text
Do While ie.ReadyState <> READYSSTATE_COMPLETE And ie.Busy
Loop
Set doc = ie.Document
Set LoginForm = doc.getElementById(Sheet1.Range("B4").Text)
Set UserNameInput = LoginForm.getElementsByClassName("prePopulatedCredentials")(0)
Set PasswordInput = LoginForm.getElementsByClassName("prePopulatedCredentials ")(1)
UserNameInput.Value = Sheet1.Range("B2").Text
PasswordInput.Value = Sheet1.Range("B3").Text
Stop
'LoginForm.submit
End Sub
An here the code for the 2nd Web Side with the Prob debug Print
Sub Get()
Dim ie As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim UserNameInput As MSHTML.HTMLInputElement
Dim VornameInput As MSHTML.HTMLInputElement
Dim HTMLButton As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim SecurityWindow As Object
Set ie = New SHDocVw.InternetExplorer
Set ie = CreateObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
ie.Visible = True
ie.Navigate Tabelle1.Range("B1").Text
Do While ie.ReadyState <> READYSSTATE_COMPLETE And ie.Busy
Loop
Set HTMLDoc = ie.Document
Set UserNameInput = HTMLDoc.getElementById("sur")
Set VornameInput = HTMLDoc.getElementById("given")
UserNameInput.Value = Sheets1.Range("B2").Text
VornameInput.Value = Sheets1.Range("B3").Text
Stop
Set HTMLButtons = HTMLDoc.getElementsByTagName("btn")
For Each HTMLButton In HTMLButtons
Debug.Print HTMLButtons.className, HTMLButtons.tagName, HTMLButtons.ID, HTMLButtons.innerText
Next HTMLButton
End Sub
What have I done wrong or missed?
Thx for any help
And.. most important
Take all of you care
Best wishes, stay safe and healthy!
Pete
For your first issue:
It looks like a timing-related issue.
I suggest you make your code wait for some seconds before executing the problematic line of code.
It can help to fetch the value and solve this issue.
For your second issue:
As I informed you in my previous comment, you can create a new thread with detailed information. We will try to check it and try to provide you further suggestions for it.

Referencing to an iframe in vba

as my previous question doesnt fit anymore, im creating a new one.
I have the following code to open an window. in that windows i want to open the sorting "function" and then select which attribute to sort.
Page opening works
What doesnt work, is trying to address the attributes of the sorting drop down menu, so that it can select the right one. i always get debug.print length = 0 in the last code line
The code:
Sub testalt()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ie As InternetExplorerMedium
Dim enumm As String
Dim ifrm As MSHTML.HTMLDocument
Dim selection As MSHTML.IHTMLElementCollection
enumm = "E2711846"
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.navigate "https://plm.corp.int:10090/enovia/common/emxFullSearch.jsp?pageConfig=tvc:pageconfig//tvc/search/AutonomySearch.xml&tvcTable=true&showInitialResults=true&cancelLabel=Close&minRequiredChars=3&genericDelete=true&selection=multiple&txtTextSearch=" & [enumm] & "&targetLocation=popup"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = ie.document
Set ifrm = HTMLDoc.frames(1).document
Set selection = ifrm.getElementsByTagName("select")
Debug.Print selection.Length
End Sub
This is the, i think, relevant html part of the opened page. Sry dont know how to copyit
this is the iframe, which is on the top

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

VBA to complete a Internet form

I'm looking for a code to put values from an Excel to a Webpage.
Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://xxxxxxxx/"
IE.Visible = True
While IE.Busy
DoEvents
Wend
IE.document.getElementById("xxxxx").Value = "xxxx"
End Sub
The error comes in on IE.document.getElementById("xxxxx").Value = "xxxx line and it says Method 'Document' of object 'IWebBrowser 2' failed.
I'm looking for suggstions to solve this question: I made a lot of research and nothing works :/
Thanks in advance :)
Ana,
Here is a working example. You can easily customized this little bit of code to work for you. One time you must be aware of is that I added a reference to my project under TOOLS -> Preferences -> Microsoft Internet Controls. Also another word of caution is on this line Set Elements on this line, if you are looking for something that does not exists you will end up with an error. This working example I put together goes to stack overflow and finds the elements below footer-menu.
Let me know if something does not make sense.
Sub TryInternetExplorer()
'Reference to system32\shdocvw.dll 'Microsoft Internet Controls
Dim IEApp As InternetExplorer
Set IEApp = New InternetExplorer
IEApp.Visible = True
IEApp.Navigate "https://iam.pearson.com/auth/XUI/#login/"
While IEApp.ReadyState <> READYSTATE_COMPLETE And IEApp.ReadyState <> READYSTATE_LOADED
DoEvents
Wend
'wait for the form to load
Do
Set form = IEApp.Document.getElementsByTagName("FORM")(0) '1st table
DoEvents
Loop While form Is Nothing
IEApp.Document.getElementsByName("callback_0")(0).Value = "Miguel"
IEApp.Document.getElementsByName("callback_1")(0).Value = "pass"
IEApp.Document.getElementsByName("callback_2")(0).Click
Set Document = Nothing
Set IEApp = Nothing
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