1

C# を使用した WebBrowser で発生する状況があります。

サイトからダウンローダーを実行しようとしていますが、最初にクリックすると機能しませんが、2回目にクリックすると機能します。

この問題を解決するにはどうすればよいですか。心から。

コード :

    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";
    }
4

1 に答える 1

2

問題が何であるかを調査することを妨げています。ホスト アプリで内部的に処理しない限り、WebBrowser( のように) コントロールのスクリプト エラーを無効にすることは決して良い考えではありません。ScriptErrorsSuppressed = true以下をせよ:

次に、ボタンのクリックをシミュレートしているときに何が問題なのかを見つけて、デバッグできることを願っています。

于 2013-10-05T20:51:28.210 に答える