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.
Related
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.
I'm using Watin library in a windows forms app. In order to hide the browser I use this instruction :
Settings.Instance.MakeNewIeInstanceVisible = false;
However, it doesn't hide the popups (when simulating a click on an element that opens a popup).
Is there a way to hide them?
You can do it programmatically by running some javascript code and make window.open function to do nothing!
Example
Here is a test page I made that has a very simple form and when the user clicks the Sum button, it sums up numA + numB and it displays the result inside a <span id="result"></span> element. After the result text update, it opens a popup window by calling window.open. In order to make this popup window disappear, we need to eliminate the window.open function:
window['open'] = function() { return false; }
To do that using Watin, we have to use the Eval function and inject the javascript code like this:
browser.Eval("window['open'] = function() { return false; }");
Then all popups are gone for that page load only and we have the wanted result.
Sample C# code
private void buttonPopupdEnabled_Click(object sender, EventArgs e)
{
WatiN.Core.Settings.Instance.MakeNewIeInstanceVisible = false;
IE ie = new IE();
ie.GoTo("http://zikro.gr/dbg/html/watin-disable-popups/");
ie.Eval("window['open'] = function() { return false; }");
ie.TextField(Find.ById("numA")).TypeText("15");
ie.TextField(Find.ById("numB")).TypeText("21");
ie.Button(Find.ById("sum")).Click();
string result = ie.Span(Find.ById("result")).Text;
ie.Close();
labelResult.Text = String.Format("The result is {0}", result);
}
Program running before javascript injection (Popup shows up)
Program running after javascript injection (Popup is gone)
I've checked released notes and found this:
By default WatiN tests make the created Internet Explorer instances
visible to the user. You can run your test invisible by changing the
following setting. Be aware that HTMLDialogs and any popup windows
will be shown even if you set this setting to false (this is default
behavior of Internet Explorer which currently can't be suppressed).
IE.Settings.MakeNewIeInstanceVisible = false; // default is true
Since WatIN haven't updated since 2011, I think you wouldn't expect any new feature support what you want.
I don't know if this could be a workaround but If those popups are not important to you why just don't block all popups?
How to turn off popup blocker through code in Watin?
HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WEBOC_POPUPMANAGEMENT
Value = 0 for Off
Value = 1 for On
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;
I have an asp.net web application that when you click a button it excecutes all my stuff but it is slow it takes 10 to 15 seconds to work and i dont want users to spam click the button while the site is busy. So my plan was to disable the button and have the label below it become buesy and say "Running" and after it was done the button would reactivate and the label would say "Done" but this dident work as planned here is my code for the button listener.
protected void SubmitButton_Click(object sender, EventArgs e)
{
SubmitButton.Enabled = false;
RunStatus.Text = "Running";
RunStatus.Visible = true;
ErrorField.Visible = false;
//i deleted all my code that actually does the stuff that takes time from here
RunStatus.Text = "Done";
SubmitButton.Enabled = true;
}
the problem is that all the visual stuff happens after the method is run so while it is running nothing changed but after it has the text feild show done. I would also be happy to have a busy cursor but that is of secondary importance.
Wrap your button in an UpdatePanel and use the UpdateProgress control to show the client that an AJAX call is taking place. See this tutorial for more information.
I'm really sorry but you have a fundamental gap in your knowledge about the ASP.NET page lifecycle. It's really very different from Windows Forms - which is what the kind of code you've posted would be applicable to.
I respectfully suggest you grab a decent book on ASP.NET and try to build up a basic understanding about ASP.NET's abstraction over HTTP/HTML.
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.