【发布时间】: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