WebBrowser.Navigate just... Isn't - c#

I have this code:
private void goButton_Click(object sender, EventArgs e)
{
web.Navigate(loginURL.Text + "/auth/login");
}
I have the browser showing, and it's just not navigating... It doesn't navigate etc.
The URL is valid.

MSDN is your friend. Make sure you have the 'http://' prefix and try using the Navigate(Uri url) overload.
// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
if (String.IsNullOrEmpty(address))
return;
if (address.Equals("about:blank"))
return;
if (!address.StartsWith("http://") && !address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webBrowser.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}

You need to handle DocumentCompleted event of web browser.
Go through following code:
private void goButton_Click(object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
wb.AllowNavigation = true;
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate(loginURL.Text + "/auth/login");
}
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
// wb.Document is not null at this point
}

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 ?

How can i understand login is succes or not on hidden webbrowser in c# webForms

its my code i dont want to see webbrowse in my design. i try to when i click button if password is correct login is success but i cannot see what happen how can i check login is succes or not without webBrowser .
For example login is succes or not just i want to see messagebox.Thanks for help.
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("http://195.2.23.56:3635/LoginPage.aspx?ReturnUrl=%2fError%2fAppError.aspx");
webBrowser1.Hide();
webBrowser1.ScriptErrorsSuppressed = true;
private void button2_Click(object sender, EventArgs e)
{
HtmlElement ele = webBrowser1.Document.GetElementById("KullaniciAdiTextBox");
if (ele == null) { ele.InnerText = "username"; } else { ele.InnerText = textBox1.Text; }
ele = webBrowser1.Document.GetElementById("SifreTextBox");
if (ele == null) { ele.InnerText = "password"; } else { ele.InnerText = textBox2.Text; }
ele = webBrowser1.Document.GetElementById("SistemeGirImageButton");
if (ele != null) { ele.InvokeMember("click"); }
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string s = webBrowser1.DocumentText;
}

web browser cannot work with 1 click

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.

WebBrowser Url to string not work

When the code reaches MessageBox.Show() it throws an exception Object reference not set to an instance of an object.
Here is my code:
WebBrowser webb = new WebBrowser();
webb.Navigate("https://www.facebook.com/logout.php?next=http://facebook.com&access_token=" + Settings.Default["token"].ToString());
MessageBox.Show(webb.Url.AbsoluteUri.ToString());
Do you see a reason why this might be the case ?
This might work:
WebBrowser webb = new WebBrowser();
private void pictureBox3_Click(object sender, EventArgs e)
{
webb.Url = new Uri("https://www.facebook.com/logout.php?next=http://facebook.com&access_token=" + Settings.Default["token"].ToString());
webb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show(webb.Url.AbsoluteUri.ToString());
}

webBrowser Navigating url

this code doesn't work why?
private void web_FBCheck_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (web_FBCheck.Url.ToString() == ("http://facebook.com/"))
{
MessageBox.Show("Welcome to Facebook");
textBox3.Text = web_FBCheck.Url.ToString();
}
}
or use if (web_FBCheck.Url.ToString() == "http://facebook.com/") without ( ) in the link
I'd guess it's because you're handling Navigating instead of Navigated, and possibly because of URL formatting. You should be doing something like this instead:
private void web_FBCheck_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
if(e.Url.Host.ToLower().IndexOf("facebook.com") > -1) {
MessageBox.Show("Welcome to Facebook");
TextBox3.Text = web_FBCheck.Url.ToString();
}
}

Categories