if (webBrowser1.Url.AbsoluteUri == "www.google.com")
{
label9.Text = webBrowser1.Url.AbsoluteUri;
}
This is my current code. When I press the button to run this I get the error.
Object reference not set to an instance of an object.
And I don't know why it does that or how to fix it. Any help will be great.
Also It have to work in a timer so that it can be checked.
The Url Property will remain null untill the control is rendered so use this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (webBrowser1.Url.ToString() == "www.google.com") {
label9.Text = webBrowser1.Url.ToString();
}
}
And in your button Click event add:
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
probably your webBrowser1.Url is null
try below to get url
string url = "";
if (webBrowser1.Url != null)
{
url = webBrowser1.Url.AbsoluteUri;
}
if (url == "www.google.com")
{
label9.Text = url;
}
Well you didn't set any url (no page is loaded inside the web browser). You could try this:
webBrowser1.Url = new Uri("http://www.google.com", UriKind.Absolute);
And get the url this way: webBrowser1.Url.ToString();
Wait for the page to load and the press then button.
I thought id comment on this, I literally took your
"webBrowser1.Url.AbsoluteUri;"
and in my case im using a combotextbox so Double click your browser form and it will take you to the even handler, i just put
"combobox1.text= webBrowser1.Url.AbsoluteUri;"
and it works for me now. You got me on the time, but whatever you need to check, check for the combobox1.text or whatever you are using for your url's
if your browser1 is chromiumwebbrowser, then use
string url = browser1.Address;
call the url and you will get it.
Related
I was asked by a friend to develop a winform app to be able to extract data. I figured it would be easy enough - how wrong I was!
In my winform, I have included a webbrowser control and some buttons. The URL for the webbrowser is http://www.racingpost.com/greyhounds/card.sd and as you can imagine, it is the place to get data for greyhounds. When on the page above, there are a number of links within this area which are specific to a race time. If you click on any of these, it takes you to that race, and its this data that I need to extract. So, my initial thoughts were to get ALL links off the link above, then store them in a list, then just have a button available to take in whatever link it is, and then take the webbrowser to that location. Once there, I can then look to extract the data and store it as needed.
So, in the first instance, I use
//url = link above
wb1.Url = new Uri(url);
grab the data (which are links for each race on that day)
once I have this, use a further button to go to the specific race
wb1.Url = new Uri("http://www.racingpost.com/greyhounds/card.sd#resultday=2015-01-17&raceid=1344640");
then, once there, click another button to capture the data, after which, return to the original link above.
The problem is, it will not go to the location present in the link. BUT, if I click the link manually within the webbrowser, it goes there no problem.
I have looked at the properties of the webbrowser, and these all look fine - although I can't qualify that tbh!
I know if I try to go to the links manually, I can, but if I try to do it through code, it just wont budge. I can only assume I have done something wrong in the code.
Hope some of that makes sense - first posting, so apologies if I made a mess of it. I will provide all code no problem, but cant seem to figure out how to post the code in 'code format'?
//here is the code
public partial class Form1 : Form
{
Uri _url;
public Form1()
{
InitializeComponent();
wb1.Url = new Uri("http://www.racingpost.com/greyhounds/card.sd");
wb1.Navigated +=new WebBrowserNavigatedEventHandler(wb1_Navigated);
}
classmodules.trackUrl tu;
private void btnGrabData_Click(object sender, EventArgs e)
{
classmodules.utility u = new classmodules.utility();
rtb1.Text = u.GetWebData("http://www.racingpost.com/greyhounds/card.sd");
HtmlDocument doc = wb1.Document;
string innerText = (((mshtml.HTMLDocument)(doc.DomDocument)).documentElement).outerHTML;
innerText = Regex.Replace(innerText, #"\r\n?|\n", "");
rtb1.Text = innerText;
tu = new classmodules.trackUrl();
u.splitOLs(ref tu, innerText);
classmodules.StaticUtils su = new classmodules.StaticUtils();
su.SerializeObject(tu, typeof(classmodules.trackUrl)).Save(#"d:\dogsUTL.xml");
classmodules.ExcelProcessor xl = new classmodules.ExcelProcessor();
xl.createExcel(tu);
}
private void wb1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb1 = sender as WebBrowser;
this.Text = wb1.Url.ToString();
}
void wb1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
_url = e.Url;
}
private void btnGoBack_Click(object sender, EventArgs e)
{
goBack();
}
private void goBack()
{
wb1.Url = new Uri("http://www.racingpost.com/greyhounds/card.sd");
}
private void btnGetRaceData_Click(object sender, EventArgs e)
{
HtmlDocument doc = wb1.Document;
string innerText = (((mshtml.HTMLDocument)(doc.DomDocument)).documentElement).outerHTML;
rtb2.Text = innerText;
}
//###############################
//OK, here is the point where I want to take in the URL and click a button //to instruct the webbrowser to go to that location. I add an initial //counter to 0, and then get the first url from the list, increment the //counter, then when I click the button again, urlNo wil be 1, so then it //tries the second url
int urlNo = 0;
private void btnUseData_Click(object sender, EventArgs e)
{
if (tu.race.Count > urlNo)
{
string url = tu.race[urlNo].url;
wb1.Url = new Uri(url);
lblUrl.Text = url;
urlNo++;
}
else
{
lblUrl.Text = "No More";
}
}
Did you try the Navigate(...) method? In theory, the behavior of Navigate and Url is the same, but I can infer that they behave a bit different.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigate(v=vs.110).aspx
I have a C# web page viewer working. What I need to do is when a button is clicked within that webpage, it changes the URL that begins with a certain string, say "xyz". I need to know how I could detect this change.
In android I simply used shouldOverrideURlLoading and had an if statement but the only URL I can retrieve is the original one I pass to start the web view.
is there a way to call DocumnetedCompleted after each new screen..There are appox. 2 button presses that get me to the screen with the important button
If you are using a WebBrowser control, you can use the Navigating event to handle when the site navigates to another url.
From there you can use the WebBrowserNavigatingEventArgs to retrieve the new URL and to stop it if you want or change the destination URL.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.AbsoluteUri.Contains("something")) {
//stais in the current page
e.Cancel = true;
//aditionally you can navigate to another URL (though it will fire this event again)
webBrowser1.Navigate(e.Url.AbsoluteUri.Replace("something", "empty"));
} else {
//continue
}
}
I think you need something like this
private void change_Url () {
var URL = Request.Url.ToString(); // get the URL
// if you meant to appened the text, then
URL = URL + "abc";
}
I have a WebBrowser control which I dinamically refresh/change url based on user input. I don't want to let the user to navigate, so I set AllowNavigation to false. This seems to be OK, however the below link is still "active":
Close Page
The issue here is: If the user clicks it, and confirms closure in the pop-up window I can't manage WebBrowser anymore. Looks like it is closed though the last page is still visible. Also I can't remove this link as the site is not managed by me.
Disable the control? Nope, I have to allow the user to highlight and copy text from the webpage.
Do I have any other option to disable literally ALL links?
#TaW: here is my code based on yours. So I have to set the url from my code and call a custom one:
button_click()
{
webBrowser1_load_URL("http://website/somecheck.php?compname=" + textBoxHost.Text);
}
Here it is the function:
private void webBrowser1_load_URL(string url)
{
string s = GetDocumentText(url.ToString());
s = s.Replace(#"javascript:window.close()", "");
webBrowser1.AllowNavigation = true;
webBrowser1.DocumentText = s;
}
The rest is exaclty what's in your answer:
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.AllowNavigation = false;
}
public string GetDocumentText(string s)
{
WebBrowser dummy = new WebBrowser(); //(*)
dummy.Url = new Uri(s);
return dummy.DocumentText;
}
Still it's not working. Please help me to spot the issue with my code.
If you have control over the loading of the pages you could grab the pages' text and change the code to disable rogue scripts. The one you showed can simply be deleted. Of course you might have to forsee more than the one..
Obviously this could be eased if you could do without javascript alltogether, but if that is not an option go for those that do real or pseudo-navigation..
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.AllowNavigation = false;
}
private void loadURL_Click(object sender, EventArgs e)
{
webBrowser1.AllowNavigation = true;
string s = File.ReadAllText(textBox_URL.Text);
s = s.Replace("javascript:window.close()", "");
webBrowser1.DocumentText = s;
}
If the pages are not in the file system, the same trick should work, for instance by loading the URL into a dummy WebBrowser like this:
private void cb_loadURL_Click(object sender, EventArgs e)
{
string s = GetDocumentText(tb_URL.Text);
s = s.Replace("javascript:window.close()", "");
webBrowser1.AllowNavigation = true;
webBrowser1.DocumentText = s;
}
public string GetDocumentText(string s)
{
WebBrowser dummy = new WebBrowser(); //(*)
dummy.Url = new Uri(s);
return dummy.DocumentText;
}
Note: According to this post you can't set the DocumentText quite as freely as one would think; probably a bug.. Instead of creating the dummy each time you can also move the (*) line to class level. Then, no matter how many changes you had to make, you would always have an unchanged version, th user could e.g. save somewhere..
I have a webbrowser control in my Winform application.
Below regions belong to division of code sample.
Region 1
The current url page loaded is "http://MyWebsite.com". I am clicking a link (say "About Us") in the web page using code. This click will take me to new url page ("http://MyWebsite.com/About_Us"). In Navigating event I am recording this new url.
Region 2
Now I want to get all elements of this new url and click on a new link. But not sure how to do it. In Region 2 I am also assigning the new url to webbrowser object. But nothing reflects in the instance. webbrowser.url still contains the previous url path.
I have following code for button click:
private void Button1Click(object sender, EventArgs e)
{
// Region 1---------------------------------------------
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText != null && link.InnerText.Equals("Click to view magic"))
{
link.InvokeMember("Click");
break;
}
}
// EndRegion---------------------------------------------
// Region 2---------------------------------------------
webBrowser1.Url = new Uri(_url.AbsoluteUri, UriKind.Absolute);
webBrowser1.Navigate(_url); //New Edit
links = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement link in links)
{
if ((link.GetAttribute("Name") == "BooHoo"))
{
link.InvokeMember("Click");
break;
}
}
// EndRegion---------------------------------------------
}
private void WebBrowser1Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
_url = e.Url;
}
Can anyone help me to do this. The question may not be very clear. Please let me know if you need any further details. Thanks.
So it was tricky a bit or else I am careless. I was watching the property values in debug mode. Later I noticed that, after pressing F5 in Visual Studio (continue debugging) and all the methods are run, webbrowser shows the changed values.
Hope it helps.
You need to subscribe Navigated event as WebBrowser works asynchronously.
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
--Do Something Here....
}
From my app MainPage I go to a website (alternatively: I go to task: send email).
When pressing 'back button' black screen is returned, instead of MainPage. I have tried to find a solution, but have not found one yet. Can anyone help?
I figured out how to solve the problem (which I had but no one else, perhaps!). If you need to make an external link do not do it directly from your Main page. Instead create an intermediate page to go to, from where you link externally (to a website or the email application, for example). Then you come back to this page with the back arrow, and using a logical string, have been here ...have not been here, you can direct back to the main page. See below:
private void Button1_Click(object sender, RoutedEventArgs e)
{
/* instead of putting the code here, you go to another page
EmailComposeTask emailcomposer = new EmailComposeTask();
emailcomposer.To = "Ä;
emailcomposer.Subject = "Customer request";
emailcomposer.Body = "Text:";
emailcomposer.Show();
*/
var b = App.Current as App;
b.Emailat = "email";
NavigationService.Navigate(new Uri("/Page2.xaml",UriKind.Relative));
}
And on this other page:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
var c = App.Current as App;
string Howisit = c.Emailat;
if (Howisit == "email")
{
EmailComposeTask emailcomposer = new EmailComposeTask();
emailcomposer.To = "";
emailcomposer.Subject = "Customer request";
emailcomposer.Body = "Text:";
emailcomposer.Show();
var b = App.Current as App;
b.Emailat = "stop";
}
if (Howisit == "stop")
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}
That's it!