簡體   English   中英

無法點擊表格選項

[英]Not able to click on an option of a table

我正在嘗試自動化使用第三方網站的任務。 在此網站中,我們必須從項目列表中選擇一個選項。

這是一個文本區域,當單擊該區域時,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”。 這沒有發生。

我最終不必擔心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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM