Im trying to build an app for myself to use to automate the downloading of mp3 from youtube. I'm using a web browser to navigate to the site, I was following a tutorial on codeproject, but the project was in visual basic, and I don't know if I converted it properly or what. I understand how to click on buttons within the site with getelementbyID and invokemember. Anyways, the first half of the code works, and it puts the url where its supposed to go, and then presses the button, but when the blue link pops up, the html is different for the link than that of a button, and i'm not sure how to click on the hyperlink through code. I'm decently confident with c# but when it comes to html and network stuff in code, I have no idea where to begin learning. Sorry for being overly complicated, here is my code.
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/");
}
}
}