如何通过标签,标签名称获取元素,然后单击标签位置(无效)//镜像iMacros程序(有效)

时间:2019-04-08 15:50:03

标签: excel vba web-scraping

目标:自动运行网络报告,保存到文件,上传到FTP

问题:无法单击VBA中的元素,但我知道需要采取的路线,因为该宏在iMacros程序(https://imacros.net/download/)中可以成功工作

问题:我如何在VBA中镜像iMacro命令,或者在VBA中编写的最佳方法是什么?我很高兴获得解决方案,并感谢您抽出宝贵的时间来帮助我!

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-工作:)

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代码-不起作用:(

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

运行时错误'438': 对象不支持此属性或方法

出于帧计数目的: enter image description here

2 个答案:

答案 0 :(得分:1)

处理框架的通常方法是获取框架对象,然后访问其中的contentDocument。例如,假设您想要的子项目在框架1中:

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

使用名称

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

看起来您像父母一样拥有框架组,例如,您可能需要降低框架组

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

答案 1 :(得分:1)

了解如何处理HTTP请求。

以下信息将使您登录:

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

登录后,您可以操纵响应HTML,该HTML存储在doc中,但是您可以。 使用的参考文献:

Microsoft WinHTTP Services, version 5.1

Microsoft HTML Object Library