【问题标题】:WebBrowser - unable to retrieve and set HTML elements in an .asp page which is calledWebBrowser - 无法在名为的 .asp 页面中检索和设置 HTML 元素
【发布时间】:2014-01-18 22:47:11
【问题描述】:

这适用于 Visual Studio Express 2012 中的桌面 C# 应用程序。

我正在使用 webBrowser 控件登录到各种网站,但是我无法检索和设置此特定网站的属性,其中登录属性位于由 HTML“调用”的 .asp 页面中。

属性本身不会通过 GetElementById 或通过 HtmlElementCollection 导航显示。

下面的 HTML sn-p 显示了对 login.asp 的调用以及我试图设置但驻留在“login.asp”中的字段“CorporateSignonCorpId”和“CorporateSignonPassword”

**Website HTML**
</head>
<frameset rows="*,0">
<frame src="login.asp" id="main" name="main" frameborder="0" noresize="true" scrolling="auto" hidefocus="true" target="_top">
<frame src="hiddenframe.asp" id="data" name="data" frameborder="0" noresize="true" scrolling="auto" hidefocus="true">
<noframes>


**login.asp:**
<!--CRN AND PASSWORD TABLE-->
    <table border="0" cellpadding="2" cellspacing=0>
        <tr>
            <td align="right" nowrap class="header5">Customer Registration Number:</td>
            <td valign="top" colspan="2"><font face="sans"><input type="TEXT" name="CorporateSignonCorpId" tabIndex=0 size="16" maxlength="19" AUTOCOMPLETE="OFF" onKeyPress="onFocus:microsoftKeyPress();" onFocus="javascript: strActiveField='CorporateSignonCorpId';"></font></td>
        </tr>
        <tr>
            <td align="right" class="header5">Password:</td>
            <td valign="top"><font face="sans"><input type="PASSWORD" name="CorporateSignonPassword" size="16" tabIndex=0 AUTOCOMPLETE="OFF" onKeyPress="javascript:microsoftKeyPress();" onFocus="javascript: strActiveField='CorporateSignonPassword';"></font></td>
        </tr>
        <tr>

如何在 C# 中引用和设置属性“CorporateSignonCorpId”和“CorporateSignonPassword”的值?

任何帮助将不胜感激! 谢谢米克

【问题讨论】:

    标签: c# html asp.net .net webbrowser-control


    【解决方案1】:

    您可以使用webBrowser.Document.Window.Frames["main"].Document 来访问内框的文档。以下对我有用:

    private async void MainForm_Load(object sender, EventArgs e)
    {
        var tcs = new TaskCompletionSource<bool>();
        WebBrowserDocumentCompletedEventHandler handler = (s, arg) => 
            tcs.TrySetResult(true);
    
        this.webBrowser.DocumentCompleted += handler;
        this.webBrowser.Navigate("http://localhost:81/frameset.html");
        await tcs.Task;
        this.webBrowser.DocumentCompleted -= handler;
    
        var frameDocument = this.webBrowser.Document.Window.Frames["main"].Document;
        var element = frameDocument.GetElementById("CorporateSignonCorpId");
        MessageBox.Show(element.OuterHtml);
    }
    

    【讨论】:

    • 您好 Noseratio - 感谢您的建议,但我收到错误:mscorlib.dll 中发生了“System.Reflection.TargetInvocationException”类型的未处理异常
    • 我在这方面取得了成功——它实现了同样的目标吗? wb.Url = wb.Document.Window.Frames[0].Url; wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbFrame_DocumentCompleted);
    • 然后我就可以导航并设置元素值了
    • @Michael1410,哪一行给了你这个错误?你真的不需要做另一个导航来访问框架的内容。 DocumentCompleted 可以触发多次,每帧一次,整个页面一次(我们对此感兴趣)。观察整个页面最可靠的方法是处理页面的 DOM onload 事件。更多信息/样品herehere
    猜你喜欢
    • 2021-08-21
    • 1970-01-01
    • 2012-04-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多