Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# web浏览器不能与单击一起使用_C#_.net_Winforms_Automation_Webbrowser Control - Fatal编程技术网

C# web浏览器不能与单击一起使用

C# web浏览器不能与单击一起使用,c#,.net,winforms,automation,webbrowser-control,C#,.net,Winforms,Automation,Webbrowser Control,我在WebBrowser中遇到了一种情况,使用C# 我试图通过一个网站下载,但是,当我第一次点击时,它不工作,但如果我点击第二次它的工作 我怎样才能解决这个问题。 真的 代码: public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { webBrowser1.Document.Ge

我在WebBrowser中遇到了一种情况,使用C#

我试图通过一个网站下载,但是,当我第一次点击时,它不工作,但如果我点击第二次它的工作

我怎样才能解决这个问题。 真的

代码:

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

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

  • 启用脚本错误(
    ScriptErrorsSuppressed=false
  • )
  • ,因此网页获得了与功能齐全的IE浏览器相同(或相近)的体验和HTML功能

然后,希望您能够在模拟按钮单击时发现问题所在,并对其进行调试

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