2014-03-01 46 views
-1

有一個頁面,我想獲取它的身體來讀取輸入區域,並通過C#中的GetAttribute和SetAttribute更改它們的值。這是沒有問題的做到這一點,但WebBrowser document.body.InnerHtml返回空?

沒有什麼回報(我的意思是空字符串),當我通過撥打體:

webBrowser1.Document.Body.InnerText 

,或者

webBrowser1.Document.Body.InnerHtml 

這就是爲什麼我無法訪問任何輸入字段。 我看到webbrowser組件中的Web頁面,但是既不InnerText也不InnerHtml返回。這是在本地運行的已保存的銀行存款。

那麼我怎樣才能讀取正文,運行SetAttribute或GetAttribute或InvokeMember別的東西?

+2

您是否等到頁面完全加載? http://stackoverflow.com/questions/20957182/load-url-from-txt-file-and-load-the-url-in-browser-synchronous-webclient-httpreq –

+0

你什麼時候打電話給webBrowser1.Document.Body。 innerText屬性。在webbrowser控件中加載頁面後立即生效? – Nps

+0

是完全loaded.How我不知道,但問題解決了。 Perhabs我錯過了一些東西。我只有幀標籤。然後,我嘗試通過代碼訪問每個框架:webBrowser1.Document.Window.Frames [i] .Document.Body.InnerHtml並且該作品 –

回答

-2

你需要讓輸入元素,然後獲取或設置文本:

HtmlElementCollection elements = currentBrowser.Document.GetElementsByTagName("INPUT"); 
    foreach (HtmlElement element in elements) 
     { 
      //to get the text use : string value = element.GetAttribute("value"); 
      //to set the text use : elemet.InnerText = "something"; 

     } 

,但不要忘了,通過上面的代碼,你得到的所有投入要素,要搜索一個特定的元素,你可以在網頁中查看其ID或名稱:例如:

if (element.Name.ToLower().Contains("email")) 
     //do work 
+0

@AliCAKIL,出於好奇,這個答案是如何解決您的問題的? –