Excel VBA click a cell in a table controlled by ja

2020-03-08 06:11发布

问题:

I am trying to create a web query for excel but I ran into a bit of a snag. I figured out how to log into the web page and click a link to go to the right portalID. However I am having trouble getting to specific reports.

To get to specific reports, I need to click their link in a table but it appears to use javascript. The HTML for this table is below. There are about 10 cells in this table but to keep the code light I only included the first 2 cells. As you can see, each cell has its own unique ID so I can easily filter to the correct cell element with VBA. However I cannot seem to figure out how to actually click the cell.

<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>

Here is my vba to direct to the cell I need to click. It looks like I need to invoke the "onmouseup" event of the table. How would I "link" the cell I need with the onmouseup event of the table?

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

回答1:

Well, while searching other vba/javascript questions I actually figured it out on my own!

I went through all the javascript code to see how the HTML events tied into the javascript and sure enough, "onmouseup" requires "onmousedown" which requires "onmouseover". Here I was trying to execute the final interaction and didn't even think of breaking down the javascript code.

So, if anyone else runs into an issue trying to click a cell in a javascript controlled table menu, you may need to execute all the ways the mouse interacts with the page depending on how the javascript was written.

Here is my final code that worked.

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