web browser cannot work with 1 click - c#

I have a situation which occurs in WebBrowser with C#.
I am trying to do downloader through a site However , when i click first time, it doesn't work but if I click second times it works.
How can i solve this problem .
Sincerely.
Codes :
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";
}

You're blocking yourself from investigating what the problem is. It's never a good idea to disable script errors for WebBrowser control (as you do with ScriptErrorsSuppressed = true), unless you handle them internally in your host app. Do the following:
enable script errors (ScriptErrorsSuppressed = false);
enable script debugging for all applications;
implement WebBrowser Feature Control (FEATURE_BROWSER_EMULATION), so the web page is getting the same (or close) experience and HTML features as with full-featured IE browser.
Then, hopefully, you can find out what's going wrong when you're simulating a button click, and debug it.

Related

WebBrowser.DocumentCompleted and Navigate - C#

I'm trying to modify a simple application which use the WebBrowser item:
private void button1_Click(object sender, EventArgs e)
{
var menu = webBrowser1.Document.GetElementById("SomeItem").InvokeMember("click");
webBrowser1.Visible = true;
button1.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Visible = false;
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("domain");
}
This works fine, the page load and then after clicking the button the user is redirected to the desired location.
Now I'm trying to make this without the help of the button.
Simply put the button code inside the load function didn't help, so I've introduced in my code the DocumentCompleted event.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("SomeDomain");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentComplete);
}
private void wb_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
wb.Document.GetElementById("SomeItem").InvokeMember("click");
}
By debugging it, I saw that the wb object is correctly instantiated with the "domain" but I got System.NullReferenceException when I try to retrieve SomeItem.
The document is not fully loaded because I'm still in the load function ?

Retrieving the URL of a clicked link in a C# WebBrowser control

Okay. So, I'm making my own internet browser and I have tabs. But, I'm trying to make it a real time url updater while if you click on any link it will show up in the textbox of the url.
It will not work.
Here is the tab button.
private void button8_Click(object sender, EventArgs e)
{
WebBrowser Browser = new WebBrowser();
tabControl1.TabPages.Add("New Page");
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
Browser.Name = "Web Browser";
Browser.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browser);
((WebBrowser)(tabControl1.SelectedTab.Controls[0])).GoHome();
And here is where my textbox isn't getting the right url.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
//textBox1.Text = Browser.Url.ToString();
((WebBrowser) (this.tabControl1.SelectedTab.Controls[0])).Url.ToString();
}
In order for this to work, you need to get all link elements of the web page you've just loaded and assign a custom function to the HtmlElement.Click event of that element.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
var linkElements = Browser.Document.GetElementsByTagName("a");
foreach(HtmlElement link in linkElements)
{
link.Click += (s, args) =>
{
// a link is being clicked
// get the url the link is pointing to using the href attribute of the element
textBox1.Text = link.GetAttribute("href");
}
}
}

Loading Image into Picturebox from URL

I'm programming an algorithm in C# to download an image from a website and my software automatically fills all the required fields and presses the download button. The problem is that I'm not able to load the image. The dialog box to download image on Internet Explorer is appearing, so I need to get the url of the picture after clicking the button "generate" and load it into the picture box. My code:
private void simpleButton6_Click(object sender, EventArgs e)
{
string i = "0";
HtmlElement hu = webBrowser3.Document.GetElementById("data-text");
hu.Focus();
hu.SetAttribute("Value", txtEncodeData.Text);
HtmlElement hu1 = webBrowser3.Document.GetElementById("color");
hu1.Focus();
Color c = customColorBlender1.SelectedColor;
string hex = c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
hu1.SetAttribute("Value", hex);
HtmlElement hu2 = webBrowser3.Document.GetElementById("effect");
hu2.Focus();
if (comboBoxEdit1.SelectedIndex == 1)
{
i = "1";
}
hu2.SetAttribute("Value", i);
webBrowser3.Document.GetElementById("generate").InvokeMember("click");
t.Interval = 1000;
t.Start();
t.Tick += new EventHandler(t_Tick);
}
void t_Tick(object sender, EventArgs e)
{
t.Stop();
String url = Convert.ToString(webBrowser3.Document.GetElementById("download").InvokeMember("click"));
MessageBox.Show(url);
pictureBox15.ImageLocation = Convert.ToString((Uri)webBrowser3.Document.GetElementById("download").InvokeMember("click"));
}

C# Webclient downloading multiple files

I have another question :(.
I'm trying to download multiple files for my application.
My question is: What do I have to do to check if the first download is done and then continue to the second download and so on?
This is my code atm:
private void DownloadBukkit()
{
MySingleton.Instance.FirstStartProgress = "Downloading Bukkit.jar... Please stand by...";
webClient.DownloadFileAsync(new Uri(MySingleton.Instance.BukkitDownloadLink), Jar_Location);
webClient.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
webClient.DownloadFileCompleted += (webClient_DownloadFileCompleted);
}
private void DownloadDll()
{
if (!webClient.IsBusy)
{
MySingleton.Instance.FirstStartProgress = "Downloading HtmlAgilityPack.dll... Please stand by...";
webClient2.DownloadFileAsync(new Uri(Dll_HtmlAgilityPackUrl), Dll_HtmlAgilityPackLocation);
webClient2.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
webClient2.DownloadFileCompleted += (webClient2_DownloadFileCompleted);
}
}
void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
DownloadDll();
}
void webClient2_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Invoke((MethodInvoker)
Close);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Invoke((MethodInvoker)
delegate
{
labelProgress.Text = MySingleton.Instance.FirstStartProgress;
progressBar1.Value = e.ProgressPercentage;
});
}
I have checked this link: DownloadFileAsync multiple files using webclient but I didn't really understand how to implant this :(. (I'm quite new to C#)
The DownloadFileCompleted event is your signal to know that you are finished downloading the file.
From MSDN:
This event is raised each time an asynchronous file download operation
completes.
It's not clear from your code, where and how webClient and webClient2 are declared but essentially, you can start your second download when the first DownloadFileCompleted event is fired. Note, however, that you can perform the download of 2 different files concurrently provided you use 2 separate instances of WebClient.
Here is the fast forward code you can change it as per your requirement .
WebClient client = null;
public FileDownloader()
{
InitializeComponent();
client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;
}
void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
lblMessage.Text = "File Download Compeleted.";
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
lblMessage.Text = e.ProgressPercentage + " % Downloaded.";
}
private void StartDownload(object sender, RoutedEventArgs e)
{
if (client == null)
client = new WebClient();
//Loop thru your files for eg. file1.dll, file2.dll .......etc.
for (int index = 0; index < 10; index++)
{
//loop on files
client.DownloadFileAsync(
new Uri(
#"http://mywebsite.com/Files/File" + index.ToString() + ".dll"
, UriKind.RelativeOrAbsolute),
#"C:\Temp\file+" + index.ToString() + ".dll");
}
}

C# WebBrowser Controll with javaScript and html

I try to use the WebBrowserControll with C#. I use the following Code.
The WebBrowser Controll open the Web Page and if there is a href= blank it is also oppend in an new WebBrowser Controll. But If I want to open an new JavaScript with window.open it doenst open an new WebBrowser Controll. What I have to do that a href= blank and a Java Script is opend in my WebBrowser Controll.
Does anyone have a Answer??
Thanks for your help.
public MainWindow()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.FileDownload += new EventHandler(webBrowser1_FileDownload);
webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);
webBrowser1.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);
this.FormClosing += new FormClosingEventHandler(webBrowser1_FormClosing);
}
public void setURL(String aURL)
{
webBrowser1.Url = new Uri(aURL);
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
// open href= blank in new WebBrowser Controll
MainWindow newWindow = new MainWindow();
newWindow.setURL(webBrowser1.StatusText);
newWindow.Show();
e.Cancel = false;
}
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Console.WriteLine(e.KeyCode.ToString() + " " + e.Modifiers.ToString());
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
{
MessageBox.Show("ctrl-v pressed");
}
}
private void webBrowser1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show(this, "Really close the window?", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
private void beendenToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
I Do not think it is possible... The browser should send a create argument (1), when clicking on a window.open, all though it does work when closing a window (destroy (2))
If you are up for a hack you could search the document on load and replace the window.open in the DocumentText property, with tags instead, it should work with a bit of creativity.
Good luck

Categories