最近用VB.NET,通過內嵌 WebBrowser1制作了一個瀏覽器桌面程式。
通過 WebBrowser1.Document.All(TextBox2.Text).SetAttribute("value", TextBox3.Text) 這段代碼,可以實作對簡單網頁中的文本框進行填寫的功能。比如,百度的搜索框,有些系統的登錄頁面。
TextBox2文本框中輸入網頁中需要賦值的Text文本框的名稱,在TextBox3文本框中輸入要填賦的值,點擊按鈕運行上面的代碼,就能填寫網頁中的文本框。
但是,在一些復雜的有框架 iframe 的頁面中,這個代碼就沒有辦法使用了,出現如下錯誤提示

這是詳細的錯誤資訊:
有關呼叫實時(JIT)除錯而不是此對話框的詳細資訊,
請參見此訊息的結尾。
************** 例外文本 **************
System.NullReferenceException: 未將物件參考設定到物件的實體。
在 WindowsApp3定向填表.Form1.Button2_Click(Object sender, EventArgs e)
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加載的程式集 **************
mscorlib
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.8793 (QFE.050727-8700)
基本代碼: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
WindowsApp3定向填表
程式集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代碼: file:///C:/Users/222222/Desktop/11111/tianbiao.exe
----------------------------------------
Microsoft.VisualBasic
程式集版本: 8.0.0.0
Win32 版本: 8.0.50727.5483 (Win7SP1GDR.050727-5400)
基本代碼: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.8791 (QFE.050727-8700)
基本代碼: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.5491 (Win7SP1GDR.050727-5400)
基本代碼: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.5495 (Win7SP1GDR.050727-5400)
基本代碼: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.8771 (QFE.050727-8700)
基本代碼: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
Microsoft.mshtml
程式集版本: 7.0.3300.0
Win32 版本: 7.0.3300.0
基本代碼: file:///C:/Windows/assembly/GAC/Microsoft.mshtml/7.0.3300.0__b03f5f7f11d50a3a/Microsoft.mshtml.dll
----------------------------------------
mscorlib.resources
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.8793 (QFE.050727-8700)
基本代碼: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
System.Windows.Forms.resources
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.5420 (Win7SP1.050727-5400)
基本代碼: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
System.Xml
程式集版本: 2.0.0.0
Win32 版本: 2.0.50727.8773 (QFE.050727-8700)
基本代碼: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
************** JIT 除錯 **************
要啟用實時(JIT)除錯,
該應用程式或計算機的 .config 檔案(machine.config)的 system.windows.forms 節中必須設定
jitDebugging 值。
編譯應用程式時還必須啟用
除錯。
例如:
<configuration>
請各位指點一下,代碼應該怎么寫呢,謝謝啦。我這個技術小白學VB.NET真是困難,網上好多類似代碼都是VB6的,還請各位老師多多指教
uj5u.com熱心網友回復:
加點延遲,看看是不是網頁輸入框還沒有來得及加載就執行了uj5u.com熱心網友回復:
webBrowser1.Document.Window.Frames[0].Document.GetElementById("id").SetAttribute("value", "");uj5u.com熱心網友回復:
明顯button2的click事件中出錯了,你使用了為空的物件。uj5u.com熱心網友回復:
我是等網頁打開后,才點擊按鍵運行代碼,代碼就那一句話
uj5u.com熱心網友回復:
謝謝adp985老師的答疑啊,我去試試


uj5u.com熱心網友回復:
如果跨域就不好搞了,cefsharp肯定是可以的uj5u.com熱心網友回復:
自己除錯一下即可,看看Document里面是否有TextBox2.Text這個物件,再說不是所有物件都有value屬性的哦uj5u.com熱心網友回復:
有iframe的網頁要定位到框架里的元素必須先定位iframe,直接按元素定位是找不到的。uj5u.com熱心網友回復:
最近家里有點事,沒顧上看帖子,不好意思啊。現在沒法逐一回復各位老師啦,不知道是不是因為過了那個時間了?謝謝asq985老師啊,我后來看了看網頁,我說的那個網頁里有好多個框架,而且還有一層框架里邊還有框架,不知道怎么用這個陳述句webBrowser1.Document.Window.Frames[0].Document.GetElementById("id").SetAttribute("value", "") ?
謝謝zj_zwl老師,我用的這個網頁只針對IE的,cefsharp不知道合適不合適,哈哈,主要我不太會用。
謝謝it_gz_xi老師,我看了,確實有VALUE屬性的。
謝謝FainSheeg老師,你說必須先定位iframe,不知道具體陳述句怎么實作呢?或者給個鏈接讓我學習學習,哈哈,謝謝啦
uj5u.com熱心網友回復:
你這個跟我出錯的很像唉,我一般是代碼寫回圈了,系統反應不過來崩了uj5u.com熱心網友回復:
定位方法如2L所說:webBrowser1.Document.Window.Frames[0].Document.GetElementById("id").SetAttribute("value", "")有多個frame的把Frames[0]里面的0改成相應的索引號。
frame里面又有frame的:webBrowser1.Document.Window.Frames[0].Document.Window.Frames[0].Document.GetElementById("id").SetAttribute("value", "") 無非是多嵌套幾層罷了。
uj5u.com熱心網友回復:
太好啦,謝謝FainSheeg老師啊,我趕緊試試這個辦法。再次謝謝FainSheeg老師




FainSheeg
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/102040.html
標籤:VB.NET
