Excel VBA to Click Ajax Link:

MattDW

Board Regular
Joined
Mar 7, 2007
Messages
66
I am working on writing some VBA code to automate filling out a ticket web form.

The piece of this I am currently stuck on is the clicking of a dynamic (search result driven) A HREF tag link that contains what I believe is some AJAX code.

On the web form, you enter an asset number into a search box and you click on the "Search" button. This brings up a list of possible matches as a search result on the page. I have automated adding the asset automatically from my Excel spreadsheet in the textbox and executing the clicking of the "Search" button. The search result link appears on the page (and because there is only one matching asset in the database, only one search result appears).

The link display text for that hyperlink is equal to the asset number and the code for this link is: < a href="javascript:void(0);" *******="AUL.update('AssignedAssetsUpdateContainer', {}, '12.7.21.0.0.0.0.2.3.0.0.1.2.0.3.0.1.5.5.0.1.3.1.5.0.0.1.2.9.5.1.19.0.1.3.1');" >168358< /a >

I've tried two approaches in trying to click this link so far:

Count = 0
For Each l In ie.Document.getElementsByTagName("a")
If l.href = "javascript:void(0);" Then
Count = Count + 1
If Count = "6" Then
l.Click
GoTo ClickTicketDetailsButton
End If
End If
Next

There are five other links on this page that contains the last five assets I've searched for and attached to tickets. The dynamic search result link should be number 6. However, while this approach works perfectly for those first five links, it fails when trying to identify and click the search result link.

The other approach I've been playing with is to execute the JavaScript ******* event directly using: Call CurrentWindow.execScript("AUL.update('AssignedAssetsUpdateContainer', {}, '280.7.21.0.0.0.0.2.3.0.0.1.2.0.3.0.1.5.5.0.1.3.1.5.0.0.1.2.9.5.1.19.0.1.3.1');")

The first number appears to be random while the other numbers remain fixed.

This approach works when I step the code and I can manually change the (what is this number called, is this a session ID?) ID to match, but as this number changes each time the page is refreshed (and possible every time the search is conducted). Is there a way through code to pull/identify this number using VBA and then insert it back into the Call CurrentWindow.execScript statement?

Here is how I am establishing my connection to the website.

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library

Sub TriggerDivClick()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim div
Dim url As String
Dim Tags As Object
Dim Tagx As Object
Dim LastName As Object
Dim Count
Dim l

'Automate The Creation Of The Web Help Desk Ticket

'URL For The Ticketing System
url = "https://help.psdschools.org/helpdesk/WebObjects/Helpdesk.woa"

'Create InternetExplorer Object
Set ie = New SHDocVw.InternetExplorer

'Make Internet Explorer Visible
ie.Visible = True

'Go to UR Specified Above
ie.Navigate url

'Wait Until Internet Explorer Is Not Busy
While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend

If there is any other info that would be helpful, please let me know.

Thanks much!

-Matt- :)
 
Last edited:

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

MattDW

Board Regular
Joined
Mar 7, 2007
Messages
66
Is there anyone who might have an idea?

I see the DIV container on this page (the one that holds the search results) contains the request ID number I need to pull. The number listed here is the same rotating/random number that the search result AUL.Update hyperlink I want to click contains.

Is there a way through VBA to pull the attribute for updateurl from this below DIV tag and then to modify the string to only include the number at the end?

<div id="AssetSearchResultsDiv" style="float:left;" updateurl="/helpdesk/WebObjects/Helpdesk.woa/ajax/12.7.21.0.0.0.0.2.3.0.0.1.2.0.3.0.1.5.5.0.1.3.1.5.0.0.1.2.9.5">

If I can do that then it should be possible to add that number to a variable and to add that unique number back to the end of this VBA code:

Call CurrentWindow.execScript("AUL.update('AssignedAssetsUpdateContainer', {}, '21.7.21.0.0.0.0.2.3.0.0.1.2.0.3.0.1.5.5.0.1.3.1.5.0.0.1.2.9.5.1.19.0.1.3.1');")

Thanks much!

-Matt- :)
 

MattDW

Board Regular
Joined
Mar 7, 2007
Messages
66
Is there anyone who might have an idea?

I see the DIV container on this page (the one that holds the search results) contains the request ID number I need to pull. The number listed here is the same rotating/random number that the search result AUL.Update hyperlink I want to click contains.

Is there a way through VBA to pull the attribute for updateurl from this below DIV tag and then to modify the string to only include the number at the end?

< div id="AssetSearchResultsDiv" style="float:left;" updateurl="/helpdesk/WebObjects/Helpdesk.woa/ajax/12.7.21.0.0.0.0.2.3.0.0.1.2.0.3.0.1.5.5.0.1.3.1.5.0.0.1.2.9.5" >

If I can do that then it should be possible to add that number to a variable and to add that unique number back to the end of this VBA code:

Call CurrentWindow.execScript("AUL.update('AssignedAssetsUpdateContainer', {}, '21.7.21.0.0.0.0.2.3.0.0.1.2.0.3.0.1.5.5.0.1.3.1.5.0.0.1.2.9.5.1.19.0.1.3.1');")

Thanks much!

-Matt- :)
 

MattDW

Board Regular
Joined
Mar 7, 2007
Messages
66
Figured it out. Thanks anyway.

For Each Tagx In Tags
If Tagx.getAttribute("id") = "AssetSearchResultsDiv" Then
Path = Tagx.getAttribute("updateURL")
RequestID = Right(Path, Len(Path) - Application.WorksheetFunction.Find("ajax/", Path) - 4)
Call CurrentWindow.execScript("AUL.update('AssignedAssetsUpdateContainer', {}, '" & RequestID & "');")
End If
Next
 

Watch MrExcel Video

Forum statistics

Threads
1,099,519
Messages
5,469,111
Members
406,637
Latest member
Mbsmbs

This Week's Hot Topics

Top