Stop Action on Navigating event of WebBrowser Control - c#

I have created a little Win Form App in C# and added the WebBrowser component to it. What i am trying to achieve is a little app that can load a local html page from a file which has "custom" protocols in it and can of course also navigate to a web address.
For example i would have entry as follows in my webpage
'Close Company</TD></TR>' which would open a task in a program.
The way i tried to achieve this was via the Navigating event as shown below
private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if ((webBrowser.StatusText.Contains("Special")))
{
//For some reason the stop doesn't do much it still tries to proceed to special:123
//diplaying can not load page..
webBrowser.Stop();
//Launch program here.
MessageBox.Show("Special Command Found");
}
}
Problem is that it still navigates and says it can't find of course the page.
I swapped Stop with GoBack which for some reason has the same issue the first time i run it and when i then select backward in the browser it works from thereon.
I also tried navigated and use of GoBack, besides having a flashing in the app from going back the event does not fire again after the first time anymore.
Has anyone any ideas how to solve this or what i am doing wrong here ?

Instead of using WebBrowser.Stop();
just set e.cancel = true;

Related

C# - Wait for a WebBrowser to completely finish navigating and loading a website/webpage

I'm trying to do web automation by creating a Windows Form application in C# using WebBrowser. I currently have the code below that navigates to Youtube and inputs a string in Youtube's search bar.
website.Navigate("www.youtube.com");
website.Document.GetElementById("search").InnerText = "Cavaliers vs Boston highlights";
However, I get a NullReferenceException in the line
website.Document.GetElementById("search").InnerText = "Cavaliers vs Boston highlights";
I tried searching in different websites on how a WebBrowser is able to determine if it has completely finished loading the website you have specified in the Navigate method but so far I haven't found any.
What I have found online are methods that checks a WebBrowser's ready state but upon trying it, it doesn't even load the Form I created, yet still proceeds to the GetElementById method.
Hoping someone can help me with this, been trying to find a solution since morning.
Try to add an event listener to WebBrowser. The WebBrowser has a WebBrowser.DocumentCompleted event that occurs when the web page has been fully loaded.
Something like
public frmMain()
{
website.DocumentCompleted += website_DocumentCompleted;
}
public void website_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
website.Document.GetElementById("search").InnerText = "Cavaliers vs Boston highlights"
}
where frmMain is your form. It could be of course added in somewhere else too.

winform app webbrowser error changes are not allowed while code is running

i am facing an issue in winform app, i am using webbrowser control. At one point i am using for loop and in the loop i have to invoke the Enter button event programatically. It is something like below:
for (int i = 1; i <= 4; i++)
{
htmlElement = webBrowser1.Document.GetElementById(PagerTxtBoxID);
htmlElement.InnerText = i.ToString();
SendKeys.Send("{ENTER}");
}
now every time at SendKeys.Send("{ENTER}"); the page should gets refreshed with new values (inside webbrowser control).
However it is not working and the page is not refreshing and when i am trying ti debug, it shows changes are not allowed while code is running.
Requesting you folks to please guide me from here.
SendKeys.Send(...) actually send the keyboard signals as when you are hitting keys on whatever is focused now. If you are on debug, it probably tries to send enter key to visual studio, modifying the C# code. You have to figure out another way of interacting with the webbrowser. One method is to programatically click a html button.

WebBrowser Control continue script execution without script error warning box

I have to call 3rd party CRM system. There are some issues in their javascript code, but webpage functions correctly in IE9.
If I have ScriptErrorsSuppressed set to false I will get error message - "An error has occurred in this script on this page" and at bottom - "Do you want to continue running scripts on this page?"
If I click "Yes" web page will work correctly, "No" on other hand will not bind jquery correctly to objects.
If I set ScriptErrorsSuppressed to true, there will be no error message, however this will stop executing script and web page will not work correctly.
IE9 can display script errors, but keep on executing script. Is there a way to emulate same behaviour?
I have tried all solutions propoused in stack overflow, but they stop script executing
Web browser control runs in IE9 mode, I set registry value to force IE9
Thank you
-Maigais
We have almost similar tasks. I tried everything including WebBrowser.ScriptErrorsSuppressed = true. And this was not what I was looking for because all JavaScripts stopped working after an exception has appeared.
But the next one worked for me:
public PageWithWebBrowser()
{
WebBrowser.DocumentCompleted += OnWebBrowserDocumentCompleted;
WebBrowser.Navigate("http://www.example.com");
}
private void OnWebBrowserDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser.Document.Window.Error += OnWebBrowserDocumentWindowError;
}
private void OnWebBrowserDocumentWindowError(object sender, System.Windows.Forms.HtmlElementErrorEventArgs e)
{
//Suppresses a dialog and continues running scripts on the page
e.Handled = true;
}
As you can see I use System.Windows.Forms.WebBrowser control for that, may be it is going to work with WPF WebBrowser control.
Hope this will help.

Winform App - Webpage Interaction

Windows Form Application – Manipulating input-elements in WinForm WebBrowser
Although I am familiar with HttpWebResponse/HttpWebRequest to login to a website, I was trying it now via using the mshtml library and found some weird behavior and I would like to see if someone else might be able to help me out here..
I have an HTML login page with a java backend with a Username field, a Password field and a Button.
The logic is very basic, I have a built a winform app with a built in webbrowser.
At the Document_Completed event I use the following code to enter my settings and to click the button.
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser.Url.ToString() == #"MyWebPage/signin")
{
HTMLDocument hdc = new HTMLDocumentClass();
hdc = (HTMLDocument)webBrowser.Document.DomDocument;
IHTMLElement elb = hdc.getElementById("login_button");
IHTMLInputElement elu = (IHTMLInputElement)hdc.getElementById("username");
IHTMLInputElement elp = (IHTMLInputElement)hdc.getElementById("password");
try
{
elu.value = "MyID";
elp.value = "MyPwd";
elb.click();
}
catch { }
}
}
Apart for this code being very quick and without error handling, it should do the trick and it does, partially..
There are two scenario's:
I launch the tool, it loads the webpage.
The tool populates the UserID field and the Password field correctly
The tool fails to click the button
I click the button manually, I am logged in, I click logout, I am back at login page
I immediatly logged in again, the tool enters the information
The tool immediatly clicks the button as well.
Is there anyone who might be able to explain me why this happens and how I could get around this with the current setup (hence not using HttpWebRequest). I don't see the difference between loading the page at startup or being redirected after logout, but apparently there is a difference in there or I am doing something wrong.
Any feedback on this matter is very much appreciated.
Thanks,
Kevin
EDIT:
I added a Button to my Windows Form that bas the same backend Code as below in order to click the button on the webpage, this works perfectly.
I triggered clicking this button in the webBrowser_Completed event but it doesn't work.
For some reason, everything I add to the webBrowser_DocumentCompleted event does not allow me to trigger the click event for the button in my WebBrowser control. Once that entire event has completed, if I then try to trigger it it works but I would like to automate this.. Any advice?
This might be a long shot and not the most elegant workaround but how about letting a backgroundworker run for a second in your DocumentCompleted event that then triggers the button that you clicked from it's seperate thread. This might just get this automated.
As this will run from a different thread, keep in mind that you might have to invoke certain controls so this might be another downside to this workaround..
If this doesn't work then, as Regfor previously suggested, Watin.org can help you out.
how about this :
HtmlElement button = webBrowser.HtmlDocument.GetElementById("login_button");
button.InvokeMember("click");
it works in my program.

Go Back in WebBrowser

I can't find any doucmentation on this. I have a WebBrowser control for my C# Windows Phone app, and I would like to have a button to take the user back one page. How can I do this? (I know how to make the button or access the physical back button, the question is just about how to make a function to take the user back one page.
Thanks.
EDIT: Also, if there's nothing to go back to, I'd like to run another function if possible.
EDIT: Tried this: browser.InvokeScript("eval", "history.go(-1)");, it's not working for some reason. It just closes the app.
EDIT: Got the above working with e.Cancel = true; but how do I restore the default function if there is no page to go back to...?
There are a couple of suggestions in this thread that may work, especially since you don't have to worry about multiple browsers. One way is to use the `history.length' property:
if(history.length > 0)
{
//go back
}
else
{
//cannot go back so perform other function
}
Another way suggested in that thread is to check the referrer. The first page doesn't usually have a referrer:
if (document.referrer == "") {
window.close()
} else {
history.back()
}
Invoke these scripts the same way you invoked the history.go() script and see if that works.

Categories

Resources