WebBrowser Url to string not work - c#

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());
}

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 to use WebBrowser in windows phone?

I'm using the following code to make my app to load a url,
WebBrowser wb = new WebBrowser();
wb.Navigate(new Uri(uri,UriKind.Absolute));
But still it is not loading that page? What could be the problem??
WebBrowser is a control. Like a button or a textblock, you won't see anything unless you put it somewhere in your page.
To launch an external browser, use WebBrowserTask:
var webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri(uri, UriKind.Absolute);
webBrowserTask.Show();
Yo can't have any callbacks with the default WebBrowserTask in wp, if you need more control, use the WebBrowser as you have been doing,
Xaml
<phone:WebBrowser IsScriptEnabled="True" LoadCompleted="UriContentLoaded" x:Name="browserControl" />
Code Behind
public MainPage() //Your page constructor
{
InitializeComponent();
this.browserControl.Loaded += SetBrowserUri;
}
private void SetBrowserUri(object sender, RoutedEventArgs e)
{
browserControl.Navigate(new Uri("http://www.bing.com"));
}
private void UriContentLoaded(object sender, NavigationEventArgs e)
{
if (MessageBox.Show("Do you want to load a second uri?", "Load again", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
browserControl.LoadCompleted -= this.UriContentLoaded; //Remove previous handler
browserControl.LoadCompleted += this.SecondUriContentLoaded; //Add new handler
browserControl.Navigate(new Uri("http://www.google.com"));
}
}
private void SecondUriContentLoaded(object sender, NavigationEventArgs e)
{
MessageBox.Show("Finished loading");
}

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.Navigate just... Isn't

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
}

Source code to text file not working using C#.net and Web browser?

This source code scraping is not working; it's giving a blank text file as output:
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.jigsaw.com/SearchCompany.xhtml?opCode=refresh&rpage=20&order=0&orderby=0&industry=1160000&subindustry=1160300&country=9000&country=2000&cmDead=false&count=0&screenNameType=0&screenName=&omitScreenNameType=0&omitScreenName=&rowsPerPage=200&uid=13473859&tok=1354716874406-8761960955252771794");
string MainsourceCode = webBrowser1.DocumentText;
StreamWriter sw = new StreamWriter("G:/jigsaw_info.txt", true);
sw.Write(MainsourceCode + "\n");
sw.Close();
}
You are starting to write too fast. You should wait DocumentCompleted event.
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
webBrowser1.Navigate("https://www.jigsaw.com/SearchCompany.xhtml?opCode=refresh&rpage=20&order=0&orderby=0&industry=1160000&subindustry=1160300&country=9000&country=2000&cmDead=false&count=0&screenNameType=0&screenName=&omitScreenNameType=0&omitScreenName=&rowsPerPage=200&uid=13473859&tok=1354716874406-8761960955252771794");
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
File.WriteAllText("G:/jigsaw_info.txt",webBrowser1.DocumentText);
}
You have to wait until navigation is complete. Navigation is asynchronous.
Detect WebBrowser complete page loading

Categories