【问题标题】:visual studio c# webbrowser interact with hyperlinkvisual studio c# webbrowser 与超链接交互
【发布时间】:2015-07-29 10:30:38
【问题描述】:

我正在尝试为自己构建一个应用程序,用于自动从 youtube 下载 mp3。我正在使用网络浏览器导航到该站点,我正在关注关于 codeproject 的教程,但该项目是 Visual Basic 的,我不知道我是否正确转换了它或什么。我了解如何使用 getelementbyID 和 invokemember 在站点内单击按钮。无论如何,代码的前半部分有效,它将url放在它应该去的地方,然后按下按钮,但是当蓝色链接弹出时,链接的html与按钮的html不同,我'不知道如何通过代码点击超链接。我对 c# 相当有信心,但是当涉及到代码中的 html 和网络内容时,我不知道从哪里开始学习。抱歉太复杂了,这是我的代码。

namespace YoutubeMP3Tool
{
 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
    }



    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.StartsWith("https://www.youtube.com/watch?v="))
        {
            button2.Enabled = true;
        }
        else
        {
            button2.Enabled = false;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {

        //following two lines work as intended
        webBrowser1.Document.GetElementById("youtube-url").SetAttribute("value", textBox1.Text);
        webBrowser1.Document.GetElementById("submit").InvokeMember("click");



        //trying to click the download link
        webBrowser1.Document.GetElementById("download").InvokeMember("click");

        button3.Enabled = true;

    }

    private void button3_Click(object sender, EventArgs e)
    {
        //this code crashes the project
        webBrowser1.Document.GetElementById("dl_link").InvokeMember("click");

        HtmlElement download_link = webBrowser1.Document.GetElementById("dl_link");
        HtmlElementCollection links = download_link.GetElementsByTagName("a");
        string link = links[0].GetAttribute("href");
        System.Diagnostics.Process.Start(link);
    }

    private void Form1_Load_1(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.youtube-mp3.org/");

    }


 }
}

【问题讨论】:

  • (1) 请格式化您在顶部书写的文字墙。 (2) 你能告诉我们你打算在上面运行这个程序的 html 的 sn-p 吗?
  • @gideon Here is a picture Link 我截取了它,因为它不允许我从检查元素复制整个内容,并且不会显示在视图页面源中。我认为这是因为每当您提供新链接时,网址都会发生变化。

标签: c# visual-studio-2010 hyperlink webbrowser-control


【解决方案1】:

根据您的屏幕截图更新,该页面没有您要查找的 id 元素。您的代码正在寻找具有 id='download'

的元素

试试

webBrowser1.Document.GetElementById("dl_link").InvokeMember("click");

我相信这应该点击<div> 然后点击超链接。

【讨论】:

    【解决方案2】:

    WebClient 将为您执行此操作。基于How to download a file from a website in C#

        private void button3_Click(object sender, EventArgs e)
        {
    
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) { 
                webBrowser1.Document.GetElementById("dl_link").InvokeMember("click");
    
                HtmlElement download_link = webBrowser1.Document.GetElementById("dl_link");
                HtmlElementCollection links = download_link.GetElementsByTagName("a");
                Uri link = new Uri(links[0].GetAttribute("href"));
                WebClient Client = new WebClient();
                Client.DownloadFileAsync(link, saveFileDialog1.FileName);
            }
        }
    

    应该适合你。您可能想要添加一个 SaveFileDialog 以允许用户选择将 MP3 保存到的位置(假设您方法中的其他代码有效,我没有测试它)。使用 DownloadFileAsync 将阻止您的 UI 挂起。

    【讨论】:

    • 好的,我用这段代码试了一下,谢天谢地没有崩溃。虽然使用保存文件对话框它只生成一个空的 mp3 文件
    • 尝试使用Client.DownloadFile() 而不是Client.DownloadFileAsync()。下载文件确实需要很短的时间,您可能需要向link 添加一个手表,以确保它已填充并填充了正确的地址。
    • 没有异步它崩溃给 403 禁止。那么问题出在链接本身吗?
    • 403 在服务器端,可能是他们阻止了你,因为你正在抓取他们的网站。你能发现错误并发布吗?
    猜你喜欢
    • 2015-06-09
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多