Home » excel » excel – How to getElement by tag, tag name, then click tag position (not working) // mirror iMacros program (working)

excel – How to getElement by tag, tag name, then click tag position (not working) // mirror iMacros program (working)

Posted by: admin May 14, 2020 Leave a comment

Questions:

Objective: automatically run web reports, save to file, upload to FTP

Problem: cannot click the elements in VBA, but I know the route I need to take because the macro successfully works in the iMacros program (https://imacros.net/download/)

Question: how can I mirror iMacro command in VBA or what is the best way to write in VBA? I am excited to get the solution and thank you for taking the time to help me out!

HTML:

<frame name="leftFrame" src:"/client1-power/utility/CommonControllerServlet?action=folderAction&subaction=getRootFolders&folderId=null" frameBorder="0" marginHeight="0" noResize="noresize" scrolling="no">

<a title="Workspaces" class="leftnavleaflink" id="itemTextLink5" onmouseover="return true;" onclick="javascript:clickOnFolder("5");" oncontextmenu="showContextMenu("5");return false;" target="mainFrame" shape="">Workspaces</a>

iMacros – Working 🙂

VERSION BUILD=12.5.503.8802
TAB T=1
TAB CLOSEALLOTHERS
'SET !PLAYBACKDELAY 0.00
URL GOTO=https://advantage.iriworldwide.com/client1-power/SplashLogin.jsp
TAG POS=1 TYPE=INPUT:TEXT ATTR=NAME:j_username CONTENT=xxxxx
TAG POS=1 TYPE=INPUT:PASSWORD ATTR=NAME:j_password CONTENT=xxxxx
TAG POS=1 TYPE=INPUT:SUBMIT ATTR=NAME:submitButton
FRAME NAME=leftFrame
TAG POS=1 TYPE=A ATTR=TXT:Workspaces
TAG POS=1 TYPE=A ATTR=TXT:"My Workspaces"
TAG POS=1 TYPE=A ATTR=TXT:XP3
TAG POS=1 TYPE=A ATTR=TXT:"Data for Interactive Edge"
TAG POS=2 TYPE=A ATTR=TXT:"Salty Snacks"
FRAME NAME=mainFrame
TAG POS=1 TYPE=A ATTR=TXT:"26 Weeks - Regions"
TAG POS=1 TYPE=A ATTR=TXT:"13 Weeks - Regions"
TAG POS=1 TYPE=A ATTR=TXT:"13 Weeks - Markets-Parent Company"
TAG POS=1 TYPE=A ATTR=TXT:"52 Weeks - Regions"

VBA Code – Not Working 🙁

Sub iriAutomation()
    Dim objIE As InternetExplorer
    Set objIE = New InternetExplorer
    objIE.Visible = True

    objIE.navigate "https://advantage.iriworldwide.com/client1-power/SplashLogin.jsp"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    objIE.document.getElementById("usrname").Value = "xxxxx"
    objIE.document.getElementById("password").Value = "xxxxx"
    objIE.document.getElementById("submitButton").Click
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    'objIE.document.getElementById("5").Click -----NOT WORKING
    'objIE.document.parentWindow.execScript "javascript:clickOnFolder(""5"");" -----NOT WORKING
    'objIE.document.HTMLDocument.getElementsByTagName ("Workspaces") -----NOT WORKING
    'objIE.document.getElementById("itemTextLink5").Click -----NOT WORKING
    'objIE.document.getElementById("5").getElementsByTagName("a").click -----NOT WORKING
    'objIE.document.HTMLDocument.getElementsByName ("leftFrame") -----NOT WORKING
End Sub

Run-time error ‘438’: Object doesn’t support this property or method

For frame counting purposes:
enter image description here

How to&Answers:

Learn how to work with HTTP requests.

The following should get you logged in:

Sub iri()
Dim req As WinHttpRequest
Set req = New WinHttpRequest
Dim doc As HTMLDocument
set doc = New HTMLDocument
Dim reqBody As String
Dim userName As String
Dim pswd As String
userName = "user"
pswd = "12345"
reqBody = "j_username=" & userName & "&j_password=" & pswd & "&login-form-type=pwd&submitButton=Login"
With req
    .Open "POST", "https://advantage.iriworldwide.com/client1-power/SplashLogin.jsp", False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send reqBody
    doc.body.innerHTML = .responseText
End With
End Sub

Once you are logged in you can manipulate the response HTML, which is stored in doc, however you please.
References used:

Microsoft WinHTTP Services, version 5.1

Microsoft HTML Object Library

Answer:

The normal approach for handling frames is to get the frame object then access the contentDocument within. For example, assuming the subitem you want is within frame 1:

objIE.document.getElementsByTagName("frame")(0).contentDocument.getElementById("itemTextLink5").click

Using the name

objIE.document.getElementsByName("leftFrame")(0).contentDocument.getElementById("itemTextLink5").click

As seems you have framesets as parents you may need to descend them e.g.

objIE.document.getElementsByTagName("frameset")(1).getElementsByTagName("frame")(1).contentDocument.getElementById("itemTextLink5").click