【问题标题】:web browser cannot work with 1 click网络浏览器无法一键运行
【发布时间】:2013-10-12 09:21:55
【问题描述】:

我有一种情况发生在使用 C# 的 WebBrowser 中。

我正在尝试通过网站进行下载但是,当我第一次点击时,它不起作用,但如果我点击第二次,它就起作用了。

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

代码:

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

【问题讨论】:

  • “它不起作用”没有提供足够的信息。会发生什么?
  • 我不知道 :) 我希望它会被下载,但它没有被下载。够了吗?因为我不知道会发生什么? :(
  • 通过调试器运行代码,看看会发生什么。
  • 真的吗?如果我知道使用调试器,我就会知道会发生什么。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# .net winforms automation webbrowser-control


【解决方案1】:

您正在阻止自己调查问题所在。禁用WebBrowser 控件的脚本错误绝不是一个好主意(就像您对ScriptErrorsSuppressed = true 所做的那样),除非您在主机应用程序内部处理它们。执行以下操作:

然后,希望您可以在模拟按钮单击时找出问题所在,并对其进行调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-31
    • 2019-11-25
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多