2013-10-05 61 views
1

我有一个情况发生在WebBrowser与C#。网页浏览器无法与1点击

我想通过网站做下载然而,当我第一次点击,它不起作用,但如果我点击第二次它的作品。

我该如何解决这个问题。 真诚。

代码:

public Form1() 
    { 
     InitializeComponent(); 
    } 


    private void button1_Click(object sender, EventArgs e) 
    { 

     webBrowser1.Document.GetElementById("youtube-url").SetAttribute("value", textBox1.Text); 
     webBrowser1.Document.GetElementById("submit").InvokeMember("click"); 
     webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 
     label3.Text = "Video Alındı , indirme işleminin hazır olması bekleniyor"; 
    } 

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("a"); 
     String link = ""; 
     foreach (HtmlElement el in col) 
     { 
      if (el.InnerText == "Download") 
      { 
       link = el.GetAttribute("href"); 
       Download(link); 
       label3.Text = "Video indiriliyor"; 
      } 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     webBrowser1.ScriptErrorsSuppressed = true; 
     webBrowser1.Navigate("http://www.youtube-mp3.org/tr"); 
    } 

    void Download(String link) 
    { 
     WebClient downloader = new WebClient(); 
     downloader.DownloadFileAsync(new Uri(link),@"D:\a.mp3"); 
     downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged); 
    } 

    void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     progressBar1.Value = e.ProgressPercentage; 
     label3.Text = "Video İndiriliyor : " + progressBar1.Value + "%"; 
     if (progressBar1.Value == 100) 
      label3.Text = "Video İndirildi"; 
    } 
+2

“它不起作用”不能提供足够的信息。怎么了? –

+0

我不知道:)我希望它会被下载,但不会被下载。 够了吗?因为我不知道会发生什么? :( – CWOmer

+0

通过你的调试器运行你的代码,并找出发生了什么 – PhoenixReborn

回答

2

你从调查的问题是什么阻止你自己。除非您在主机应用程序中内部处理它们,否则禁止WebBrowser控件的脚本错误(与您使用ScriptErrorsSuppressed = true时一样)不是一个好主意。请执行以下操作:

然后,希望您可以在模拟按钮单击并调试时找出发生了什么问题。