Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Selenium or WebBrowser alternative mimic human

Hi all I used Selenium some time ago to create a program to carry out automated actions on a website I enjoy using.

I managed to use Selenium to do what I wanted before without much trouble the only issue I had was using it in the background.

I couldn't use it without it effecting other things I was doing on the PC, I did think of using virtual machines but I would like to try and avoid this.

Last night I was playing around with the WebBrowser class in C# and its nice but limited, I like how it was self contained within the windows form application so this is what I am looking for.

Dose anyone know the best way integrating a visual representation of a browser within a windows form application but still allow me to mimic key entry etc but would run in the background.

I have heard of things like WaitN, GekoFX, MozNet etc but from what I read I am not sure any of these would work.

like image 792
Someone Avatar asked Feb 25 '13 09:02

Someone


2 Answers

In general, when you are attempting to automate a web page using a browser, you have two options for simulating user events. You can either simulate them via JavaScript, or you can use OS-level mechanisms (so-called "native events") for simulating mouse and keyboard events. Both approaches have their pitfalls.

Simulated events using JavaScript only would probably allow the window being automated to remain in the background, without system focus, while carrying out the tasks you desire. Selenium RC used this method, and Selenium WebDriver offers the ability to use simulated events for Firefox and IE. However, there are some drawbacks to this approach. Simulated events may lack the fidelity and accuracy you require. For example, "drop-down menus" on a page that work via the CSS :hover pseudoselector cannot be triggered via JavaScript, so this approach is doomed to failure in these cases. Additionally, since you're using JavaScript, you're restricted to the JavaScript sandbox, which means that cross-domain frames and the like may be strictly out of bounds.

Native events, on the other hand, are far more closely representational of a user's actual mouse and keyboard operations. In general, they'll allow the correct events to fire on the web page, and in the correct order, without you having to guess which events to fire on which elements. The downside to using them is that to implement them correctly, the window being automated must have the system focus to receive the events properly. You can attempt to hack around this using the SendMessage API if you're on Windows, but this is the Wrong Thing to do, as it's error prone and absolutely not guaranteed to work. The correct way to use native events is to use the SendInput API, but that API sends the input to the window with the system focus. WebDriver defaults to using native events for simulating user input, but it defaults to the flawed SendMessage approach. For IE, at least, it does provide an option to use the more correct SendInput approach.

If you're dead set on not requiring a browser window in the foreground, you really ought to look into a headless option. PhantomJS is a great option, and WebDriver also has a driver for it, which means you can still write your automation code in C#. Otherwise, you're limited to one of the other approaches outlined above.

like image 132
JimEvans Avatar answered Oct 12 '22 23:10

JimEvans


Does the application need to be hosted within a window?

I have used selenium, Watin for automation, unfortunately they do interfere with what you are doing and I have not managed to find a way around this.

I have used the .Net WebBrowser class too, but for automating I am not sure without testing if it is a fully featured IE, with regard to JavaScript running inside it. I think it does execute JavaScripts though, but you would need to check.

If you do not need to see what is happening there are headless options available too, even for Selenium I think: Is it possible to hide the browser in Selenium RC?

Here is a list of headless versions if that is viable for you:

https://gist.github.com/evandrix/3694955

like image 37
Mr. Mr. Avatar answered Oct 13 '22 00:10

Mr. Mr.