我正在尝试为excel创建网络查询,但遇到了一些麻烦。我弄清楚了如何登录到网页并单击链接以转到正确的portalID。但是,我在获取特定报告时遇到了麻烦。
要获取特定报告,我需要在表格中单击其链接,但该链接似乎使用了javascript。该表的HTML如下。该表中大约有10个单元格,但是为了保持代码亮起,我只包括了前2个单元格。如您所见,每个单元都有自己的唯一ID,因此我可以轻松地使用VBA过滤到正确的单元元素。但是,我似乎无法弄清楚如何实际单击该单元格。
<table submenu='1' border='0' cellpadding='6' cellspacing='0'
id='ctl02n4c73594f1bf740b982340da2a792ff62_MainM'
class="ctl02n4c73594f1bf740b982340da2a792ff62Island"
style=' background:#141311; border-width:0px; width:100%;
cursor:Default;' onselectstart="javascript:igmenu_selectStart();"
onmouseover="javascript:igmenu_mouseover(this, event);"
onmouseout="javascript:igmenu_mouseout(this, event);"
onmousedown="javascript:igmenu_mousedown(this, event);"
onmouseup="javascript:igmenu_mouseup(this, event);"igLevel='0'>
<tr>
<td align='center' id='ctl02n4c73594f1bf740b982340da2a792ff62_1'
igTag='4eddf201-f6ed-4e11-b2ff-6a742128909c'
class="n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Class"style="
white-space: nowrap; ;" igHov='n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Hover'
igTop='1'>Welcome</td>
<td align='center' id='ctl02n4c73594f1bf740b982340da2a792ff62_2'
igTag='e0d4474e-87f8-42cc-ab47-38751029052d'
class="n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Class"style="
white-space: nowrap; ;" igHov='n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Hover'
igTop='1'>Dashboard</td>
</tr>
</table>
这是我的vba,可直接转到需要单击的单元格。看来我需要调用表的“ onmouseup”事件。如何将所需的单元格与表的onmouseup事件“链接”?
Sub clickTable(toFind As String, ie As Object)
'"toFind" is the ID of the cell to find, ie is the IE application
Dim ieDoc As Object 'ieDocDocument
Dim tdCollection As Object 'table that has the javascript events and contains
the cells I want to click
Dim cell As Object 'specific "clickable" cell in the table to "click"
Set ieDoc = ie.document
Set tdCollection = ieDoc.getelementbyID("ctl02n4c73594f1bf740b982340da2a792ff62_MainM").getElementsByTagName("td")
For Each cell In tdCollection
If cell.ID = toFind Then
cell.Click
Exit Sub
End If
Next
End Sub
好吧,在搜索其他vba / javascript问题时,我实际上是自己想出来的!
我遍历了所有javascript代码,以查看HTML事件如何绑定到javascript中,并且肯定的是,“ onmouseup”需要“ onmousedown”,而“ onmouseover”需要“ onmouseover”。在这里,我试图执行最终的交互,甚至没有想到要分解javascript代码。
因此,如果其他任何人在尝试单击javascript控制的表格菜单中的单元格时遇到问题,则可能需要执行鼠标与页面交互的所有方式,具体取决于javascript的编写方式。
这是我工作的最终代码。
Sub clickTable(toFind As String, ie As Object)
'toFind is the ID of the element to find, ie is the IE application
Dim ieDoc As Object 'ieDocDocument
Dim tdCollection As Object 'table that has the javascript attributes and contains the element I want to click
Dim cell As Object 'specific "clickable" cell in the table to test
Set ieDoc = ie.document
Set tdCollection = ieDoc.getelementbyID("ctl02n4c73594f1bf740b982340da2a792ff62_MainM").getElementsByTagName("td")
For Each cell In tdCollection
If cell.ID = toFind Then
cell.FireEvent ("onmouseover")
cell.FireEvent ("onmousedown")
cell.FireEvent ("onmouseup")
Exit Sub
End If
Next
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句