无法单击表格的选项

时间:2018-03-12 13:26:57

标签: c# internet-explorer browser-automation

我正在尝试自动执行使用第三方网站的任务。在本网站中,我们必须从项目列表中选择一个选项。

这是一个文本区域,当单击它时,JS会创建一个包含选项的表。我已经能够单击文本区域以显示此列表,并可以正确识别列表中的项目。问题是,当我点击这些项目时,没有任何事情发生。

表格如下:

<div class="MenuTableContainer">
    <table class="MenuTable" style="width: 175px;" cellSpacing="0" cellPadding="0">
        <tbody class="MenuTableBody">
            <tr class="MenuTableRow">
                <td class="MenuEntryName" noWrap="">1-Extensive/Widespread</td>
                <td class="MenuEntryNoSub" ARValue="1-Extensive/Widespread"></td>
            </tr>
            <tr class="MenuTableRow">
                <td class="MenuEntryName" noWrap="">2-Significant/Large</td>
                <td class="MenuEntryNoSub" ARValue="2-Significant/Large"></td>
            </tr>
            <tr class="MenuTableRow">
                <td class="MenuEntryName" noWrap="">(clear)</td>
                <td class="MenuEntryNoSub" ARValue=""></td></tr>
        </tbody>
    </table>
</div>

我尝试填写的输入字段看起来像没有任何值:

<div class="df arfid1000000163 ardbnImpact EnumSel" id="WIN_2_1000000163" style="left: 10px; top: 82px; width: 292px; height: 21px; z-index: 990;" ardbn="Impact" artype="EnumSel" arid="1000000163" arlbox="0,4,112,17" arwindowid="2">
    <label class="label f9" id="label1000000163" style="left: 0px; top: 4px; width: 112px; height: 17px;" for="x-arid_WIN_2_1000000163">
        Impact*
    </label>
    <div class="selection" style="left: 117px; top: 0px; width: 175px; height: 21px;" arselmenu='[{ci:1000,v:"1-Extensive/Widespread"},{ci:2000,v:"2-Significant/Large"},{ci:3000,v:"3-Moderate/Limited"},{ci:4000,v:"4-Minor/Localized"}]'>
        <input title="" class="text " id="arid_WIN_2_1000000163" style="left: 0px; top: 0px; width: 154px; height: 21px;" type="text" readOnly="">
        <a class="btn btn3d selectionbtn" style="left: 154px; top: 0px; width: 21px; height: 21px;" href="javascript:">
            <img class="btnimg" alt="" src="../../../../resources/images/mt_sprites.gif">
        </a>
    </div>
</div>

如果选择了一个值,就像这样:

<div class="df arfid1000000163 ardbnImpact EnumSel" id="WIN_2_1000000163" style="left: 10px; top: 82px; width: 292px; height: 21px; z-index: 990;" ardbn="Impact" artype="EnumSel" arid="1000000163" arlbox="0,4,112,17" arwindowid="2">
    <label class="label f9" id="label1000000163" style="left: 0px; top: 4px; width: 112px; height: 17px;" for="x-arid_WIN_2_1000000163">
        Impact*
    </label>
    <div class="selection" style="left: 117px; top: 0px; width: 175px; height: 21px;" arselmenu='[{ci:1000,v:"1-Extensive/Widespread"},{ci:2000,v:"2-Significant/Large"},{ci:3000,v:"3-Moderate/Limited"},{ci:4000,v:"4-Minor/Localized"}]'>
        <input title="2-Significant/Large" class="text " id="arid_WIN_2_1000000163" style="left: 0px; top: 0px; width: 154px; height: 21px;" type="text" readOnly="">
        <a class="btn btn3d selectionbtn" style="left: 154px; top: 0px; width: 21px; height: 21px;" href="javascript:">
            <img class="btnimg" alt="" src="../../../../resources/images/mt_sprites.gif">
        </a>
    </div>
</div>

唯一的区别是输入标记中title属性的值。

我尝试设置属性(然后读取它以确认),但是一旦单击了保存按钮,它就会失败,因为该字段为空:

        sImpactString="2-Significant/Large";
        oImpactField.setAttribute("title", sImpactString);
        string attr = oImpactField.getAttribute("title");

我也试过点击TR和TD标签但没有成功。我没有在HTML中看到任何对JS函数的引用,所以我在这里缺少了一些东西。

我正在使用SHDocVw.InternetExplorer和IHtmlElement / IHtmlElementCollection。

这就是我进入TD和TR标签的方式:

 IHTMLDocument3 document = driver.Document as IHTMLDocument3;
    IHTMLElement oTableBody = null;
    bool settingsShown = false;
    do
    {
        IHTMLElementCollection tBodys = document.getElementsByTagName("tbody");
        foreach (IHTMLElement elem in tBodys)
        {
            if (string.Compare(elem.className, "MenuTableBody") == 0)
            {
                settingsShown = true;
                oTableBody = elem;
                break;
            }
        }
    } while (!settingsShown);

    int childrenPos = (int)impactRating;
    // Get the TR
    IHTMLElement oCorrectValue = oTableBody.children[0] as IHTMLElement;
    // Get the first TD
    IHTMLElement oColumn = oCorrectValue.children[0] as IHTMLElement
    //Clicking does nothing on the TR
    oCorrectValue.click()
    //Nor in the TD
    oColumn.click()

在查看JS之后我遇到了这两个函数。第一个处理鼠标事件,第二个尝试点击一个项目

MenuElement.prototype.HandleMouseEvent = function (d, h) {
    var g = h.element;
    var f = g.className;
    if (f == null) {
        return
    }
    if (d == 1) {
        if (g.tagName != "TD" || f.indexOf("MenuEntry") == -1) {
            return
        }
        this.EndScroll();
        g = g.parentNode.firstChild;
        this.HighlightElement(g);
        this.CheckOpenSubMenu()
    }
    else {
        if (d == 0) {
            if (f.indexOf("MenuEntry") != -1) {
                OpenedMenu.CheckClick()
            }

        }
        else {
            if (d == 2) {
                if (g.tagName != "DIV" && (f != "MenuScrollDown" || f != "MenuScrollUp")) {
                    return
                }
                var b = (f == "MenuScrollUp") ? 1 : -1;
                OpenedMenu.EnsureTopElement(this.mSerial);
                this.Scroll(b)
            }
            else {
                if (d == 3) {
                    if (g.tagName != "DIV" && (f != "MenuScrollDown" || f != "MenuScrollUp")) {
                        return
                    }
                    OpenedMenu.EnsureTopElement(this.mSerial);
                    this.EndScroll()
                }
                else {

                }

            }

        }

    }
    return
}

TableContextMenu.prototype.CheckClick = function () {
    var d = this.mElementStack;
    this.mCtrlFldId = null;
    var b = d[d.length - 1].mHighlightedElement;
    if (b != null && b.nextSibling != null) {
        this.mCtrlFldId = b.nextSibling.getAttribute("arbtnid")
    }
    TableContextMenu.superclass.CheckClick.call(this)
}

我现在很清楚,我需要与表中的TD进行交互。但是,我不确定是否需要先强调一个元素。要突出显示它们,我只需将鼠标移到它上面即可。有没有办法重新创建这个?然后我只需要点击TD?

我在MenuEntryName上尝试了以下代码(因为那是一个突出显示的代码),并且没有突出显示它。

        IHTMLElement entryName = desiredRow.children[0] as IHTMLElement;
        entryName.onclick = entryName.onmouseover;
        entryName.click();

一旦突出显示,TD的类别分别变为“MenuEntryNameHover”和“MenuEntryNoSubHover”。哪个没有发生。

1 个答案:

答案 0 :(得分:0)

我最后并没有担心JS而只是像这样向窗口发送密钥:

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

public void MyMethod(InternetExplorer driver)
{
        //.....
        while (currentPos++ <= nPosition)
        {
            SetForegroundWindow((IntPtr)driver.HWND);
            System.Windows.Forms.SendKeys.SendWait(Down_Key);
            Task.Delay(100).Wait();
        }
        SetForegroundWindow((IntPtr)driver.HWND);
        System.Windows.Forms.SendKeys.SendWait(Enter_Key);
        SetForegroundWindow((IntPtr)driver.HWND);
        System.Windows.Forms.SendKeys.Flush();
}