Clicking Table Row in HTML Table using VBA

无人久伴 提交于 2020-01-02 23:16:08

问题


I am attempting to create an automation tool in Excel using VBA which runs and downloads a database query from a website portal. The tool is almost completely functional, but the one step I just cannot figure out is the query selection.

Once the user has searched for the query in the portal, applicable queries are put into rows within a HTML table. There are two ways a user can run a query: double-click a row OR click a row and then click another button in the portal. My difficulty is that I am unable to preform either of these tactics even after hours of research.

The HTML code for the first row of the table (which is the desired query) is seen below. I included the td tags so you could also see the cells within the row.

<TABLE class="x-grid-table x-grid-table-resizer" style="WIDTH: 1867px" cellSpacing=0 cellPadding=0 border=0>
    <TBODY>
        <TR class="x-grid-row " _nodup="30806" viewIndex="0" viewRecordId="ext-record-17" boundView="objectgridview">
            <TD class=" x-grid-cell x-grid-cell-gridcolumn-1085   x-grid-cell-first" rowSpan="1" colSpan="1">
                <DIV class="x-grid-cell-inner " style="TEXT-ALIGN: left">
                    <SPAN class="grid-icon view-16"></SPAN>
                    <SPAN>QUERYNAME</SPAN> 
                </DIV>
            </TD>
            <TD class=" x-grid-cell x-grid-cell-gridcolumn-1086   " rowSpan="1" colSpan="1">
                <DIV class="x-grid-cell-inner " style="TEXT-ALIGN: left">QUERYTYPE</DIV></TD>
            <TD class=" x-grid-cell x-grid-cell-gridcolumn-1087   " rowSpan="1" colSpan="1">
                <DIV class="x-grid-cell-inner " style="TEXT-ALIGN: left">QUERYDESCRIPTION</DIV>
            </TD>
            <TD class=" x-grid-cell x-grid-cell-gridcolumn-1088   " rowSpan="1" colSpan="1">
                <DIV class="x-grid-cell-inner " style="TEXT-ALIGN: left">QUERYLIBRARY</DIV>
            </TD>
            <TD class=" x-grid-cell x-grid-cell-gridcolumn-1089   " rowSpan="1" colSpan="1">
                <DIV class="x-grid-cell-inner " style="TEXT-ALIGN: left">QUERYOWNER</DIV>
            </TD>
            <TD class=" x-grid-cell x-grid-cell-datecolumn-1090    x-grid-cell-last" rowSpan="1" colSpan="1">
                <DIV class="x-grid-cell-inner " style="TEXT-ALIGN: left">QUERYDATE</DIV>
            </TD>     
        </TR>
    </TBODY>
</TABLE>

I also looked at the HTML in Chrome, with the only other difference being that the tr classname switches to "x-grid-row x-grid-row-over" when the mouse cursor is over that row and "x-grid-row x-grid-row-selected x-grid-row-over-focused" when the row is clicked.

As mentioned I have tried a number of methods to click on this row. I was able to use getElementsByTagName is identify the row/table/others element, but even after finding this I still couldn't find success.

Some of the code lines that I have used are below.

IE.document.getElementsByTagName("tr")(42).Classname = "x-grid-row x-grid-row-selected x-grid-row-focused"
IE.document.getElementsByTagName("table")(20).Rows(0).Click
IE.document.getElementsByTagName("tr")(42).getElementsByTagName("td")(3).getElementsByTagName("div")(0).Click
IE.document.getElementsByTagName("tr")(42).getElementsByTagName("td")(3).Selected = True
Call IE.document.getElementsByTagName("tr")(42).execScript("mouseup()", "Javascript")

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys ("-{F10}")
WshShell.SendKeys "{DOWN}"
WshShell.SendKeys "{ENTER}"

I am hoping someone might be able to provide some knowledge that I am missing. Thanks!


回答1:


I'm not really sure whether this applies to your situation, but I've been dealing with the same issues in kendo-ui. I use the following code to select the first row in a kendoGrid.

       ie.Document.parentWindow.execScript "$('#drgdLease').data('kendoGrid').select('tr:eq(6)');"

Look further up in your server code, above the "table" tag, for an "ID", maybe as far up as the "div" element, and try something like:

       ie.Document.parentWindow.execScript "$('#yourId').data('x-grid').select('tr:eq(42)');"

I think your best option is to figure out to make "execScript" work. It took me a long time to get that one line, and I had help ! Good Luck




回答2:


Did you try using combination of attribute selectors with FireEvent?

ie.document.querySelector("[_nodup='30806'][viewindex='0']").FireEvent "ondblclick"

This targets the first row and use a double click. Do those attributes also change?




回答3:


I have never attempted to double click, so if this doesn't work we may need to replace the second .click to click the other element on your webpage.

This code should iterate through ALL the rows on the table. You can modify if needed.

Code:

Option Explicit

Sub GrabRows()

    Dim ie As InternetExplorer, doc As HTMLDocument

    'Ensure you have code in place to navigate and
    'ensure the page is fully loaded here.

    Dim myTbl As HTMLTable, tRows As HTMLTableRow, tRow As HTMLTableRow

    Set doc = ie.document
    Set myTbl = doc.getElementsByClassName("x-grid-table x-grid-table-resizer")(2)
    Set tRows = myTbl.getElementsByTagName("TR")(2)

    '____Save as a backup____
    'For Each tRow In tRows
    '    tRow.Click
    '    tRow.Click
    'Next tRow
    '------------------------

    'Try each of these together and see if you get desired result
    tRow.Click
    tRow.setActive
    tRow.Focus


End Sub

One of two things needs to happen. You need to make references to Microsoft Internet Controls and Microsoft HTML Object Library, or declare all of the library-specific objects (such as InternetExplorer & HTMLDocument) as type Object



来源:https://stackoverflow.com/questions/48233644/clicking-table-row-in-html-table-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!