I have been working on a project and noticed the watin library freezing the application. Here is the code:
using(var browser = new IE(url))
{
if(!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password))
{
browser.Link(Find.ById("overridelink")).Click();
}
}
The code above works fine if the ID "overidelink" attribute is found. If its not found the whole application freezes up. How can I prevent this?
First check if it's exists and then excecute the click
if (Browser.Link(Find.ById("overridelink")).Exists)
{
Browser.Link(Find.ById("overridelink")).Click();
}
Related
I have a program that reads some data from a website, then clicks a link on that website and reads data again from the new site after navigation.
Everything works fine as long as the program is started with admin rights. The problem occurs when I start the program without admin rights. Here is some code:
void ReadHTML(string url)
{
try
{
InternetExplorer ie = new InternetExplorer();
IWebBrowserApp wb = (IWebBrowserApp)ie;
wb.Visible = false;
wb.Navigate(main.pathHcmOverview, null, null, null, null);
while (wb.Busy) ; // Here the program crashes already with exit code 0x800706ba
HTMLDocument doc = (HTMLDocument)wb.Document;
// Do something with the doc [I cut the code here as it is not relevant for the problem]
wb.Quit();
}
catch (Exception err)
{
}
}
The navigation command works, but the next command throws an error: "RPC Server not available - 0x800706ba".
Now my question: Can I use the code above only with admin rights? I did not find any information on that yet.
And if so, is there an alternative to accomplish my goal without admin rights?
I was able to resolve the problem by changing the following line
InternetExplorer ie = new InternetExplorer();
into
InternetExplorerMedium ie = new InternetExplorerMedium();
Tests worked fine after that. I will read into the documents now about the exact differences between those two.
This question already has answers here:
How can I get the WebBrowser control to show modern contents?
(4 answers)
Closed 6 years ago.
My webbrowser control displays an intranet site. It was working fine, until the admin changed a setting in iis that forces ie11 to render in Edge mode. Now my webbrowser control comes up with the script error "object doesn't support property or method attachEvent."
Yes, I know attachEvent is deprecated in ie11. No, I do not have control over the webpage code. No, I can't force the admin to change the setting back again.
I tried using registry settings for my application under browser emulation, using all the codes starting with ie9 up through ie10. None of them had any effect.
Can anyone tell me how to force my webbrowser control to render in such a way as to avoid that script error and continue logging in? The call to attachEvent must be called upon successful login, because when I used bad credentials on the login page the error doesn't come up. When my application was working, the page defaulted to ie 9 compatibility. But it seems the admin's IIS setting has nullified that.
Since you cannot alter the code, I recommend implementing a attachEvent/detachEvent polyfill, like this
HTMLElement.prototype.attachEvent = function(event, cb) {
var onEventName = "on" + event,
obj = this;
if (obj.addEventListener) {
obj.addEventListener(event, cb, false);
} else if (obj.attachEvent) {
obj.attachEvent(onEventName, cb);
} else {
var currentEventHandler = obj[onEventName];
obj[onEventName] = function() {
if (typeof currentEventHandler === 'function') {
currentEventHandler.apply(obj, arguments);
}
cb.apply(obj, arguments);
};
}
};
HTMLElement.prototype.detachEvent = function(event, cb) {
var onEventName = "on" + event,
obj = this;
if (obj.removeEventListener) {
obj.removeEventListener(event, cb, false);
} else if (obj.detachEvent) {
obj.detachEvent(onEventName, cb);
} else {
delete obj[onEventName];
}
};
Here's a working plnkr demonstration (apologies for not using a snippet). I based this code using this old git.
Do note the code is incomplete and not for production, e.g. it is not checking whether HTMLElement, attachEvent, detachEvent exist.
I have an application that monitors a file, and based on its contents launches a browser using this code:
Process.Start("iexplore", "-nomerge " + fullUrl);
Now as with many small projects, the requirements have changed. The change is that only one browser can be launched from my program at a time.
Also, another program is capable of launching a browser with another url, and I cannot shut it down, eliminating the option of just closing down all instances of iexplore and then launching mine. ( which is what I did originally )
Is there a way to launch a browser and continue to keep control of it so you can update the URL of that specific instance of iexplore using c#?
This code is close to what is in the answer on the duplicate post, but is slightly different, so I am sharing it here.
foreach (SHDocVw.InternetExplorer ie in new SHDocVw.ShellWindowsClass())
{
if (ie.FullName.ToLower().Contains("iexplore") &
ie.LocationURL.ToLower().Contains("&qtype=mine"))
{
ie.Visible = true;
ie.Navigate(fullUrl);
openNewBrowserWindowWindow = false;
}
}
if (openNewBrowserWindowWindow) {
SHDocVw.InternetExplorerClass IE = new SHDocVw.InternetExplorerClass();
IE.Visible = true;
IE.Navigate(fullUrl);
}
I have a usb connected MSR reader and i am trying to get it by using the sample codes proveded in here. This works fine but the problem is when i add the same code to my app it doesn't work. GetDefaultAsync returns null.
private static MagneticStripeReader _reader = null;
public static async void StartRead()
{
if (await CreateDefaultMagneticStripeReaderObject())
{
....
}
}
private static async Task<bool> CreateDefaultMagneticStripeReaderObject()
{
if (_reader == null)
{
_reader = await MagneticStripeReader.GetDefaultAsync();
if (_reader == null)
return false;
}
return true;
}
My code is like above, very similer to sample but it doesnt work. Also i've added the device capability of pointOfService. So that is not the case.
I was in the exact same situation and I spent the last 5 hours, finally I know what was going on. You are missing a capability in the Package.appxmanifest
'pointOfService' is the capability you want to include. This capability does not show in the UI and therefore I could not find any difference between my broken project and Microsoft's sample project. You can not add that capability using the UI. You have to manually add it by modifying the XML file.
The sample project by Microsoft have it too
https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/MagneticStripeReader/cs/Package.appxmanifest#L53
Make sure the card reader is in HID mode and not Keyboard emulation mode. That was one of my problems.
To do this is really wonky. MagTek has a ActiveX control on their website to assist us... because ActiveX is awful, you can only use it with InternetExplorer (it won't even work with Edge.)
go here in IE: https://www.magtek.com/changemode/
Enable active X when it pops up, and you can change from hid to keyboard and back.
In visual studio 2010, working with c#;
I open a browser with:
private IE browser;
private void Set_Browser()
{
string splashUrl = "google.com";
browser= new IE(splashUrl);
}
If a user(person) closes the browser by accident, then my application will not be able to work anymore.
QUESTIONS:
So how do I check if a user closed the browser manually?
Can I unable a user from closing the browser by adding the browser to my
application GUI as a control? [using Windows Forms]
-> How do I do that?
Last question related to this post How to use watin with WebBrowser control? (2 years old, but no decent answer too)
EDIT: The solution in give URL seems to work. Problem is that if I try to send the WebBrowser.ActivateX.. as an object to other class. Then my browser = new IE(..) returns null. It does work when I instantiate it in the form class though. Any solutions?
You can search for the process of internet explorer every x seconds and see if the browser is already running using this code:
bool isRunning = false;
foreach (Process clsProcess in Process.GetProcesses()) {
if (clsProcess.ProcessName.Contains("iexplore"))
{
isRunning = true;
break;
}
}
You can use this article
Or you can add a browser control to your application using this article
One thing you can do is hide the browser to avoid users closing it .. See this SO question.
Hiding Internet Explorer when WatiN is run