I am new to WebBrowser library.
When fetching data from https://www.binance.com/indexSpa.html website, I used the following code. But it does not return all the data. What should I do?
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += Wb_DocumentCompleted;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(#"https://www.binance.com/indexSpa.html#");
private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var wb = (WebBrowser)sender;
Console.WriteLine(wb.DocumentText);
}
Thank you for help.
Related
I tried two method:
wb.Navigate(Url,true); When i use this webbrowser appear but WebBrowserDocumentCompleted event doesn't fire after page loaded.
wb.Navigate(Url); When i use this WebBrowserDocumentCompleted event is firing but browser doesn't appear on screen. How can i display external browser and fire WebBrowserDocumentCompleted event same time?
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.DocumentCompleted += WebBrowserDocumentCompleted;
wb.Visible = true;
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate("http://www.google.com",true);
}
private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if ((sender as WebBrowser).ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete)
{
// Do what ever you want to do here when page is completely loaded.
}
}
when i put Instagram's address in my WebBrowser object, on loading of program, it just shows a blank white page, and nothing else, where is problem?!
first i think it should be cause WebBrowser using IE, but IE(ver.11) loads Instagram successfully .
UPDATE:
this is the code which i use:
this.webB.Location = new System.Drawing.Point(93, 17);
this.webB.MinimumSize = new System.Drawing.Size(20, 20);
this.webB.Name = "webB";
this.webB.Size = new System.Drawing.Size(642, 324);
this.webB.TabIndex = 1;
this.webB.Url = new System.Uri("https://www.instagram.com/", System.UriKind.Absolute);
this.webB.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webB_DocumentCompleted);
this.webB.ScriptErrorsSuppressed = true;
Try this one :). This code is useful for loading urls(ex:Instagram) and download the source code of the appropriate page.
protected void LoadUrl(object sender, EventArgs e)
{
string url = txtUrl.Text.Trim();
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.Width = 1024;
browser.Height = 768;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
string sourcecode=browser.documentText;
}
This foreach loop checks a webpage and sees if there are any images then downloads them. How do i stop it? When i press the button it continues the loop forever.
private void button1_Click(object sender, EventArgs e)
{
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted +=browser_DocumentCompleted;
browser.Navigate(textBox1.Text);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
int count = 0; //if available
int maximumCount = imgCollection.Count;
try
{
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
webClient.DownloadFile(url, url.Substring(url.LastIndexOf('/')));
count++;
if(count >= maximumCount)
break;
}
}
catch { MessageBox.Show("errr"); }
}
use the break; keyword to break out of a loop
You do not have an infinite loop, you have an exception that is being thrown based on how you are writing the file to disk
private void button1_Click(object sender, EventArgs e)
{
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate("www.google.ca");
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
string name = System.IO.Path.GetFileName(url);
string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
webClient.DownloadFile(url, path);
}
}
That code works fine on my environment. The issue you seemed to be having was when you were setting the DownloadFile filepath, you were setting it to a value like `\myimage.png', and the webclient could not find the path so it threw and exception.
The above code drops it into the current directory with the extension name.
Maybe the Event browser.DocumentCompleted cause the error, if the page refreshes the event gets fired again. You could try to deregister the event.
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
browser.DocumentCompleted -= browser_DocumentCompleted;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
string name = System.IO.Path.GetFileName(url);
string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
webClient.DownloadFile(url, path);
}
}
So I have a custom windows form with a tabbed browser. I want to be able to see the hoovered hyperlink in the status bar. I have searched everywhere but nothing yet.
I checked the MSDN under the web browser class which led me to the StatusTextChanged.
So in my code I have
wb.StatusTextChanged += MainWindow_StatusChange;
then
private void MainWindow_StatusChange(Object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
this.toolStripStatusLabel2.Text = wb.StatusText;
}
Well, the tool.StripStatusLabel2.Text is empty.
Any help?
You have subscribed the wrong event.
Do this...
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
wb.StatusTextChanged += new EventHandler(wb_StatusTextChanged);
wb.Navigate("http://www.google.de");
}
void wb_StatusTextChanged(object sender, EventArgs e)
{
this.toolStripStatusLabel2.Text = ((WebBrowser) sender).StatusText;
}
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.DocumentStream = new FileStream("C:\a.html", FileMode.Open, FileAccess.Read);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
wb.Print();
I know how to set the page orientation from a PrinterDocument object, but not from a WebBrowser object. Any way to do this? Thanks!
First, I recommend you to use async event model:
wb.DocumentCompleted += wb_DocumentCompleted;
private void wb_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
((WebBrowser)sender).Print();
}
To print (add the reference to Microsoft.mshtml.dll):
mshtml.IHTMLDocument2 doc = wb.Document.DomDocument as mshtml.IHTMLDocument2;
doc.execCommand("print", showUI, templatePath);
See IHTMLDocument2.execCommand, MSDN forum question and follow links.