2015-10-18 70 views
1

我想知道一種在Windows窗體的瀏覽器中刪除網站元素的方法。我有這樣的代碼:如何從Windows窗體瀏覽器控件中刪除元素

namespace Browser 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      webBrowser1.Document.GetElementById("ads").Style = "display:none"; 
      webBrowser1.Document.GetElementById("navigation").Style = "display:none"; 
      webBrowser1.Document.GetElementById("donate").Style = "display:none"; 
      webBrowser1.Document.GetElementById("social_bookmarking_buttons").Style = "display:none"; 
     } 
    } 
} 

正如你可能會注意到,我只是隱藏的元素,我想刪除他們。謝謝你的時間。

回答

2

這適用於我刪除元素。

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    webBrowser1.Document.GetElementById("ads").OuterHtml = ""; 
    webBrowser1.Document.GetElementById("navigation").OuterHtml = ""; 
    webBrowser1.Document.GetElementById("donate").OuterHtml = ""; 
    webBrowser1.Document.GetElementById("social_bookmarking_buttons").OuterHtml = ""; 
} 
+0

謝謝,這是工作。但我有一個問題,我正在處理的這個網站有一個通知,只有在打開網站後5秒內出現。如果我在文檔完成時將其刪除,則不會刪除該通知。你能幫助這個嗎? –

+0

如果我是你,我會使用一個全局'System.Windows.Forms.Timer'對象來通知5秒鐘已過去。我將在'DocumentCompleted'中開始時間,並檢查通知是否未被刪除,然後刪除'Timer.Tick'事件處理程序中的元素。 – jhmt

+0

謝謝。我已經解決了這個問題。你能幫助我解決這個問題嗎?:http://stackoverflow.com/questions/33199399/changing-the-position-of-html-elements-in-windows-forms-webrbrowser。 –

相關問題