【问题标题】:Clicking a JavaScript link on website单击网站上的 JavaScript 链接
【发布时间】:2018-06-13 21:41:42
【问题描述】:

我正在尝试通过 VBA 从我的银行网站自动生成报告。

我成功地访问了该网站并输入了我的用户名和密码,没有任何问题。我已到达通常可以生成报告的页面。 现在我必须模拟点击链接...但我做不到。

这是网站的截图,我必须点击链接的 HTML 代码来生成报告(摘要报告副本)。

这是我需要点击的链接的 HTML 代码:

<a id="irCustomStepOne:headerDataTableId:23:reportnamestatus" href="#"  style="float:left;" title="Summary Report Copy" onclick="jsf.util.chain(this,event,'if(isDateValidForMassPayPD()==\'false\' &amp; isWareHouseRptPD(\'23\') ==\'true\'){RichFaces.$(\'irCustomStepOne:panelWareHouseRptError\').show(event, {\'top\':\'100px\', \'left\':\'300px\'});return false;}else{if(!onReportLink(\'23\')) return false;}','mojarra.jsfcljs(document.getElementById(\'irCustomStepOne\'),{\'irCustomStepOne:headerDataTableId:23:reportnamestatus\':\'irCustomStepOne:headerDataTableId:23:reportnamestatus\',\'repId\':\'3368127\',\'dayTypeKey\':\'PREVIOUS_DAY\',\'userAction\':\'Run\'},\'_blank\')');return false" class="">Summary Report Copy</a>

我试过这行 VBA 代码,但是不行:

IE.Document.getElementById("irCustomStepOne:headerDataTableId:23:reportnamestatus").FireEvent "onclick"

感谢 VBA,我如何才能点击此“摘要报告副本”?

这里是链接所在网页部分的完整 HTML 代码。也许我没有使用正确的 ID?

 <td width="290px">
 <span id="irCustomStepOne:headerDataTableId:23:reportNmGrp">
 <a id="irCustomStepOne:headerDataTableId:23:reportnamestatus" href="#" style="float:left;" title="Summary Report Copy" onclick="jsf.util.chain(this,event,'if(isDateValidForMassPayPD()==\'false\' &amp; isWareHouseRptPD(\'23\') ==\'true\'){RichFaces.$(\'irCustomStepOne:panelWareHouseRptError\').show(event, {\'top\':\'100px\', \'left\':\'300px\'});return false;}else{if(!onReportLink(\'23\')) return false;}','mojarra.jsfcljs(document.getElementById(\'irCustomStepOne\'),{\'irCustomStepOne:headerDataTableId:23:reportnamestatus\':\'irCustomStepOne:headerDataTableId:23:reportnamestatus\',\'repId\':\'3368127\',\'dayTypeKey\':\'PREVIOUS_DAY\',\'userAction\':\'Run\'},\'_blank\')');return false">Summary Report Copy</a>
 <a id="irCustomStepOne:headerDataTableId:23:reportnamePrint" href="#" style="float:left;display:none;" title="Summary Report Copy" onclick="jsf.util.chain(this,event,'if (!onReportLink(\'23\')) return false;','mojarra.jsfcljs(document.getElementById(\'irCustomStepOne\'),{\'irCustomStepOne:headerDataTableId:23:reportnamePrint\':\'irCustomStepOne:headerDataTableId:23:reportnamePrint\',\'repId\':\'3368127\',\'dayTypeKey\':\'PREVIOUS_DAY\',\'userAction\':\'Print\'},\'_blank\')');return false">Summary Report Copy</a>
 <span id="irCustomStepOne:headerDataTableId:23:rpId" style="float:left;display:none;">3368127</span>
 </span>
 </td>

【问题讨论】:

  • 你试过IE.Document.getElementById("irCustomStepOne:headerDataTableId:23:reportnamestatus").click吗?
  • 是的,我试过了,但我得到:运行时错误 '424' Object Required 我不明白,因为这是链接的 ID...
  • 在导航到网站后尝试等待几秒钟,看起来文档没有完全加载。在链接点击行上方添加:Application.Wait (Now + TimeValue("0:00:08"))
  • 同样的事情,我已经放了这行代码以确保所有内容都已加载。很抱歉浪费您的时间,我之前应该告诉过您的 ;-) 还有其他触发此链接的想法吗?

标签: javascript html vba excel


【解决方案1】:

这一直对我有用。请尝试一下:

For each lnk in IE.Document.getElementByTagName("a") 
If lnk.ID = "irCustomStepOne:headerDataTableId:23:reportnamestat‌​us" Then
    lnk.Click
    Exit For
Next

【讨论】:

  • 这也给了我“需要运行时错误'424'对象”。我把完整的html代码贴上来,方便大家更好的理解网站的结构。
【解决方案2】:

您可能需要等待页面完全加载。像

Do: VBA.DoEvents: Loop While IE.Busy ' wait for the page to load
Dim el 'As IHTMLElement
Set el = IE.Document.getElementById("irCustomStepOne:headerDataTableId:23:reportnamestat‌​us")
If el Is Nothing Then el = IE.Document.querySelector("a[title='Summary Report Copy']")
If el Is Nothing Then MsgBox("Link not found!") : Exit Sub
el.click
Do: VBA.DoEvents: Loop While IE.Busy ' wait for next page to load

【讨论】:

  • 您好 Slai,非常感谢您的帮助。您的代码给了我一个“需要运行时错误'424'对象”。我将把完整的html代码贴上来,以便我们更好地了解网站的结构。也许我试图点击错误的链接??
  • 在工具 > 参考中...检查“Microsoft Internet 控件”和“Microsoft HTML 对象库”。然后你需要像Dim IE As New SHDocVw.InternetExplorer
猜你喜欢
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 2020-10-19
  • 2020-06-11
  • 2011-06-07
  • 2022-12-29
相关资源
最近更新 更多