Search for link and click it - html

I have managed to get it work with SendKeys. However it is not the best solution to navigate web page with SendKeys. I have been trying to get this code working with no success. It opens the web page and nothing happens.
Link class: class="a_1_610"
Link text: Dashboard
From source code:
<tr>
<td class="nowrap"><span class="h_1_610" style="display: none;">Dashboard</span>
<a href="DA3192.html" class="a_1_610" >
Dashboard</a>
</td>
</tr>
DA3192.html is changing all the time. Next time you navigate to web page it is DA3672.html or something.
The problem is that all the links have class="a_1_610". So I have tried to catch it by link text, but it does not seem to work (nothing happens). Website is not available for public, only private use so there is no possibility to share it. Are there any other ways to make it work other than SendKeys?
Sub GoToWebsiteTest()
Dim appIE As InternetExplorerMedium
'Set appIE = Nothing
Dim objElement As Object
Dim objCollection As Object
Set appIE = New InternetExplorerMedium
sURL = "http://mysite/"
With appIE
.Navigate sURL
.Visible = True
End With
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set objCollection = appIE.Document.getElementsByTagName("a")
For Each hyper_link In objCollection
If hyper_link.innerText = "Dashboard" Then
hyper_link.Click
Exit For
End If
Next
Set appIE = Nothing
End Sub

you can try to see if innertext is with "bug" and have blank caracteres , use LIKE and wildcard.
For Each hyper_link In objCollection
If vba.Lcase(hyper_link.innerText) like "*dashboard*" Then
hyper_link.Click
Exit For
end if
Tell me if it works!

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

How to get excel VBA to find a name then click the hyperlink

This is the piece I am stuck on, everything else is working.
Sub Avidie()
Dim i As Long
Dim url As String
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
Set links = .document.getelementbyclass("invoice-number-hyperlink").getelementsbyname("25407")(0).Click
I am up to the point where I have the Webpage code, which is below, and I want my code to find the link that has that class and name, and click it. How can I fix this?? The class name is the same on multiple parts of this page, but the name Is what I’m looking up so I need to find it by that.
<a class="invoice-number-hyperlink" onclick="avid.navigateHelper['PAQ-Invoice'](this);" href="#/invoices/70c2373a-ac71-43f2-a9e0-8d0332f1a19b?fromQueue=true">25407</a>
Based on the part of the code you provided, as well as the methods that other members of the community have mentioned, you can traverse all eligible s according to the class, filter according to the text content, and then select and click. I have created a simple example, I hope it can be helpful to you:
Dim appIE As InternetExplorerMedium
Set appIE = New InternetExplorerMedium
sURL = "https://www.example.com/"
With appIE
.navigate sURL
.Visible = True
End With
Do While appIE.Busy Or appIE.readyState <> 4
DoEvents
Loop
'appIE.document.getElementsByClassName("invoice-number-hyperlink")(0).Click
For Each link In appIE.document.getElementsByClassName("invoice-number-hyperlink")
If link.Text = "25407" Then
link.Click
Exit For
End If
Next
And page:
<a class="invoice-number-hyperlink" onclick="window.alert('jump to google...')" href="https://www.google.com/">25407</a>

Microsoft Access VBA clicking a button on intranet site

I have been stuck on this code for about a week now. I have an access form that when completed will take the user to the reference claim number on our intranet site. While on that site, I would like one more step to be completed which is to go to the file note section where the user can paste their information. Below is my code.
Private Sub Command21_Click()
Dim appIE As InternetExplorerMedium
'Set appIE = Nothing
Dim objElement As Object
Dim objCollection As Object
Set appIE = New InternetExplorerMedium
sURL = "Intranet" & [claim]
With appIE
.Navigate sURL
.Visible = True
End With
While appIE.ReadyState <> 4
DoEvents
Wend
Set ele = appIE.Document.getElementById("btnFileNotes_unselected")
ele.Click
Set appIE = Nothing
End Sub
I get an
Run Time Error: 91
on the ele.click. Any help would be much appreciated.
Thank you.

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