【问题标题】:Not able to click on an option of a table无法单击表格的选项
【发布时间】:2018-08-20 13:03:11
【问题描述】:

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

这是一个文本区域,当它被点击时,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”。这没有发生。

【问题讨论】:

    标签: c# internet-explorer browser-automation


    【解决方案1】:

    我最终不用担心 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();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      相关资源
      最近更新 更多