2013-08-06 75 views
0

我有這樣的代碼:的getElementById不工作

web.Document.GetElementById("idLoginUserName").SetAttribute("value", user); 

用戶是一個字符串,網絡是一個WebBrowser控件。它的下

private void web_Navigated(object sender, WebBrowserNavigatedEventArgs e) 

這是頁面上的ID:

<input tabindex="100" type="text" name="username" id="idLoginUserName" value="" class="textbox "> 

然而,我得到這個錯誤:

A first chance exception of type 'System.NullReferenceException' occurred in MyProgram.exe 

An exception of type 'System.NullReferenceException' occurred in MyProgram.exe but was not handled in user code 

調試它,我可以看到,它並試圖執行它,並且在嘗試時返回該錯誤。

+2

你有沒有試過用'DocumentCompleted'事件,而不是'Navigated',爲應答[這裏](http://stackoverflow.com/questions/18048920/webbrowser-isnt-wokring)和[這裏](HTTP:/ /stackoverflow.com/a/18050820/1906557) – I4V

+0

是的,在它沒有工作後,我將它從DocumentCompleted中更改。 – Minicl55

+0

你能確定哪個值是'null'嗎?調試器很方便。 – rutter

回答

0

檢查web是否爲空。

檢查web.document是否爲空。

檢查web.Document.GetElementById(「idLoginUserName」)是否返回null。

檢查用戶是否爲空。

它必須是其中之一,或者錯誤來自其他地方。

相關問題