簡體   English   中英

C# WebBrowser 控件 - 使用 InvokeMember("Click") 提交表單不工作

[英]C# WebBrowser Control - Form Submit Not Working using InvokeMember("Click")

我正在研究自動化測試腳本並使用 WebBrowser 控件。 我正在嘗試提交以下 HTML 並在用戶接受服務條款時進行測試:

    <form action="http://post.dev.dealerconnextion/k/6hRbDTwn4xGVl2MHITQsBw/hrshq" method="post">
        <input name="StepCheck" value="U2FsdGVkX18zMTk5MzE5OUgFyFgD3V5yf5Rwbtfhf3gjdH4KSx4hqj4vkrw7K6e-" type="hidden">
        <button type="submit" name="continue" value="y">ACCEPT the terms of use</button>
        <button type="submit" name="continue" value="n">DECLINE the terms of use</button>
    </form>

    // Terms of Use Information

    <form action="http://post.dev.dealerconnextion/k/6hRbDTwn4xGVl2MHITQsBw/hrshq" method="post">
        <input name="StepCheck" value="U2FsdGVkX18zMTk5MzE5OUgFyFgD3V5yf5Rwbtfhf3gjdH4KSx4hqj4vkrw7K6e-" type="hidden">
        <button type="submit" name="continue" value="y">ACCEPT the terms of use</button>
        <button type="submit" name="continue" value="n">DECLINE the terms of use</button>
    </form>

這里是代碼在C#,但是沒有提交表單。

            HtmlElementCollection el = webBrowser.Document.GetElementsByTagName("button");
            foreach (HtmlElement btn in el)
            {
                if (btn.InnerText == "ACCEPT the terms of use")
                {
                    btn.InvokeMember("Click");
                }
            }

任何幫助將非常感激。 謝謝。

以下代碼適用於我,使用來自問題評論的實時表單操作URL,使用IE10進行測試。 按原樣試試吧。 如果它也適合您,請隨意將其用作Web自動化任務的模板。 幾點:

  • FEATURE_BROWSER_EMULATION用於確保WebBrowser行為方式與獨立IE瀏覽器相同(或盡可能接近)。 對於幾乎所有基於WebBrowser的項目都是必須的。 我相信這有助於解決您身邊的原始問題。

  • 異步代碼用於提高自動化邏輯可靠性,添加支持超時和取消,並促進自然線性代碼流(使用async/await )。

C#

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebAutomation
{
    // http://stackoverflow.com/q/19044659/1768303

    public partial class MainForm : Form
    {
        WebBrowser webBrowser;

        // non-deterministic delay to let AJAX code run
        const int AJAX_DELAY = 1000;

        // keep track of the main automation task
        CancellationTokenSource mainCts;
        Task mainTask = null;

        public MainForm()
        {
            SetBrowserFeatureControl(); // set FEATURE_BROWSER_EMULATION first

            InitializeComponent();

            InitBrowser();

            this.Load += (s, e) =>
            {
                // start the automation when form is loaded
                // timeout the whole automation task in 30s
                mainCts = new CancellationTokenSource(30000);
                mainTask = DoAutomationAsync(mainCts.Token).ContinueWith((completedTask) =>
                {
                    Trace.WriteLine(String.Format("Automation task status: {0}", completedTask.Status.ToString()));
                }, TaskScheduler.FromCurrentSynchronizationContext());
            };

            this.FormClosing += (s, e) =>
            {
                // cancel the automation if form closes
                if (this.mainTask != null && !this.mainTask.IsCompleted)
                    mainCts.Cancel();
            };
        }

        // create a WebBrowser instance (could use an existing one)
        void InitBrowser()
        {
            this.webBrowser = new WebBrowser();
            this.webBrowser.Dock = DockStyle.Fill;
            this.Controls.Add(this.webBrowser);
            this.webBrowser.Visible = true;
        }

        // the main automation logic
        async Task DoAutomationAsync(CancellationToken ct)
        {
            await NavigateAsync(ct, () => this.webBrowser.Navigate("http://localhost:81/test.html"), 10000); // timeout in 10s
            // page loaded, log the page's HTML
            Trace.WriteLine(GetBrowserDocumentHtml());

            // do the DOM automation
            HtmlElementCollection all = webBrowser.Document.GetElementsByTagName("button");
            // throw if none or more than one element found
            HtmlElement btn = all.Cast<HtmlElement>().Single(
                el => el.InnerHtml == "ACCEPT the terms of use");

            ct.ThrowIfCancellationRequested();

            // simulate a click which causes navigation
            await NavigateAsync(ct, () => btn.InvokeMember("click"), 10000); // timeout in 10s

            // form submitted and new page loaded, log the page's HTML
            Trace.WriteLine(GetBrowserDocumentHtml());

            // could continue with another NavigateAsync
            // othrwise, the automation session completed
        }

        // Get the full HTML content of the document
        string GetBrowserDocumentHtml()
        {
            return this.webBrowser.Document.GetElementsByTagName("html")[0].OuterHtml;
        }

        // Async navigation
        async Task NavigateAsync(CancellationToken ct, Action startNavigation, int timeout = Timeout.Infinite)
        {
            var onloadTcs = new TaskCompletionSource<bool>();
            EventHandler onloadEventHandler = null;

            WebBrowserDocumentCompletedEventHandler documentCompletedHandler = delegate
            {
                // DocumentCompleted may be called several time for the same page,
                // beacuse of frames
                if (onloadEventHandler != null || onloadTcs == null || onloadTcs.Task.IsCompleted)
                    return;

                // handle DOM onload event to make sure the document is fully loaded
                onloadEventHandler = (s, e) =>
                    onloadTcs.TrySetResult(true);
                this.webBrowser.Document.Window.AttachEventHandler("onload", onloadEventHandler);
            };

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(ct))
            {
                if (timeout != Timeout.Infinite)
                    cts.CancelAfter(Timeout.Infinite);

                using (cts.Token.Register(() => onloadTcs.TrySetCanceled(), useSynchronizationContext: true)) 
                {
                    this.webBrowser.DocumentCompleted += documentCompletedHandler;
                    try 
                    {
                        startNavigation();
                        // wait for DOM onload, throw if cancelled
                        await onloadTcs.Task;
                        ct.ThrowIfCancellationRequested();
                        // let AJAX code run, throw if cancelled
                        await Task.Delay(AJAX_DELAY, ct);
                    }
                    finally 
                    {
                        this.webBrowser.DocumentCompleted -= documentCompletedHandler;
                        if (onloadEventHandler != null)
                            this.webBrowser.Document.Window.DetachEventHandler("onload", onloadEventHandler);
                    }
                }
            }
        }

        // Browser feature conntrol
        void SetBrowserFeatureControl()
        {
            // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx

            // FeatureControl settings are per-process
            var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

            // make the control is not running inside Visual Studio Designer
            if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
                return;

            SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode()); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
        }

        void SetBrowserFeatureControlKey(string feature, string appName, uint value)
        {
            using (var key = Registry.CurrentUser.CreateSubKey(
                String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
                RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
            }
        }

        UInt32 GetBrowserEmulationMode()
        {
            int browserVersion = 7;
            using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
                RegistryKeyPermissionCheck.ReadSubTree,
                System.Security.AccessControl.RegistryRights.QueryValues))
            {
                var version = ieKey.GetValue("svcVersion");
                if (null == version)
                {
                    version = ieKey.GetValue("Version");
                    if (null == version)
                        throw new ApplicationException("Microsoft Internet Explorer is required!");
                }
                int.TryParse(version.ToString().Split('.')[0], out browserVersion);
            }

            UInt32 mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10.
            switch (browserVersion)
            {
                case 7:
                    mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
                    break;
                case 8:
                    mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
                    break;
                case 9:
                    mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
                    break;
                default:
                    // use IE10 mode by default
                    break;
            }

            return mode;
        }
    }
}

http://localhost:81/test.html

<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
</head>
<body>
    <form action="<the URL from OP's comments>" method="post">
        <input name="StepCheck" value="U2FsdGVkX18zMTk5MzE5OUgFyFgD3V5yf5Rwbtfhf3gjdH4KSx4hqj4vkrw7K6e-" type="hidden">
        <button type="submit" name="continue" value="y">ACCEPT the terms of use</button>
        <button type="submit" name="continue" value="n">DECLINE the terms of use</button>
    </form>
</body>

這對我有用,如下。 這可能對某人有用。

首先,我在獲得焦點時為button元素創建一個事件處理程序。 一旦所有其他表單元素都填充了適當的值,您應該將焦點放在按鈕上,如下所示:

HtmlElement xUsername = xDoc.GetElementById("username_txt");
HtmlElement xPassword = xDoc.GetElementById("password_txt");
HtmlElement btnSubmit = xDoc.GetElementById("btnSubmit");
if (xUsername != null && xPassword != null && btnSubmit != null)
{
    xUsername.SetAttribute("value", "testUserName");
    xPassword.SetAttribute("value", "123456789");
    btnSubmit.GotFocus += BtnSubmit_GotFocus;
    btnSubmit.Focus();
}

然后事件處理程序實現如下:

private void BtnSubmit_GotFocus(object sender, HtmlElementEventArgs e)
{
    var btnSubmit = sender as HtmlElement;
    btnSubmit.RaiseEvent("onclick");
    btnSubmit.InvokeMember("click");
}

在我的例子中,我也無法通過簡單地調用找到的元素的 Click 方法來單擊元素。 有效的是 Ali Tabandeh 在上面的回答中列出的類似解決方案:

  1. 找到所需的 html 元素
  2. 定義適當的 GotFocus 事件處理程序
  3. 然后調用找到的元素的 Focus 方法。

GotFocus 的事件處理程序應該

  1. 引發事件“點擊”
  2. 調用“點擊”方法

問題是這在我的案例中從第 3 次開始起作用(調用 htmlelement.Focus() 需要 3 次)。

暫無
暫無

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

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