1

I have a situation which occurs in WebBrowser with C#.

I am trying to do downloader through a site However , when i click first time, it doesn't work but if I click second times it works.

How can i solve this problem . Sincerely.

Codes :

    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";
    }
John Saunders
  • 157,405
  • 24
  • 229
  • 388
CWOmer
  • 101
  • 1
  • 2
  • 10

1 Answers1

2

You're blocking yourself from investigating what the problem is. It's never a good idea to disable script errors for WebBrowser control (as you do with ScriptErrorsSuppressed = true), unless you handle them internally in your host app. Do the following:

Then, hopefully, you can find out what's going wrong when you're simulating a button click, and debug it.

Community
  • 1
  • 1
noseratio
  • 56,401
  • 21
  • 172
  • 421