Simulating mouse movement, without browser "head", to web server - c#

I need to connect to a website programmatically, but without rendering the browser view, and then query the "headless" browser for certain page element's coordinate positions and send JavaScript events to simulate the movement of a mouse across the page form its current location to the new location.
I already have found a method that "realistically" moves the mouse programmatically between two points. All I need to do now is find a library that will allow me to connect to a page without the GUI but includes the positions of the page elements, but still query the JavaScript of the page and send JavaScript to the page's browser instance.
I know this very specific, but it will allow us to simulate our pages and test PHP heat mapping of the mouse location. It is also vital that this is for C#. Does such a library exist for C#?

Have you looked at Selenium? This can be run in headless mode, and has a very powerful API. I've used in C# for automated testing (including Javascript manipulation), and it works very well. It should be fine for scraping too though.

Related

IUIAutomation.ElementFromPoint does not give precise value for browser controls

I wrote a C# program to find the information about controls at a point. To do this, I call ElementFromPoint method of IUIAutomation. It gives IUIAutomationElement. From that I get properties of the element, such, as CurrentControlType, CurrentName, CurrentBoundingRectangle.
This works well in applications like MS Word, Notepad. But when I try to find information of an element in a browser, it only identifies the document, which is the client area of the browser. So, even if I try to get information about a button in the client area, it only identifies that it is the client area. It does not identify the button.
I tested in IE, Chrome and Firefox. In all these browsers, I am facing this problem.
Please let me know how to get precise information about the control at a point inside a browser.
If not UI automation, is there any way I can write a C# program to get the information about the control under mouse cursor in a browser the way "Inspect" of Chrome finds? Please note that the browser will not be running inside my application.

Running Javascript Off a Webpage Through C#

I have a bunch of webpages where I need to grab some product information but the webpages are all written using javascript. So there will be something like
<a href="javascript:__getInfo('content_abc')">Product Name<a>
And once that's clicked all the page's content will change (but not the html address). How can I programmatically execute that script and get all the loaded content through a C# script?
While this is the wrong approach to your problem, there is a solution that might work.
By using automated testing tools, such as Selenium, it is possible to use the web driver interface to take control of a web browser and interact with elements on the screen, click buttons and check for results.

Moving Picture Box depending on monitor size? C#

I am using a picture box in my C# application, I want it to cover a certain portion of the web browser (the username part on youtube).
I have a 21.5" monitor and this is what it looks like to me:
But then this is what it looks like to one of my users with a 24" monitor:
As you can see the position of the picture box has moved up due to that persons screen size (I believe)
Is there a way to make sure that it will always be over that section of the web browser or moving it to that section of the web browser?
Thanks.
I am convinced your approach is wrong and would break anytime either for screen resolution or size changes, or for using the mouse-wheel to zoom in/out the page or whatever. it is just unreliable and patching this by overlapping another UI control like a picture box or a panel on top of what you want to hide is simply insecure and unreliable.
I think the tow real options you have are these:
You try to interpret the page content and remove from the page's DOM the information you do not want to show to the user (eventually HTML Agility Pack could help for this DOM parsing and manipulation but I am not sure if you can read what the WebBrowser control is showing and inject changes into it);
use the YouTube APIs and Tools - .NET APIs to load the videos and details you want to load and show but rendering this information with your specific UI elements in your windows forms application, without using a browser to show the normal YouTube site.
Probably the second option takes more work but is more secure, I am not sure 100%, as I said, if the first option is viable at all. You could search for HTML Agility Pack and web browser control to see if anybody has done this before already :)

Browsers and Windows Messaging

I was asking myself how the browsers are working. How does the browser tell to the OS to change the mouse pointer from arrow to hand(IDC_HAND) for example. In desktop application I know that are used windows messages(right) but how it is happening in browsers? Spy++ doesn't seems to catch any of the mouse pointer messages in this case. Can you help me with an explanation?
I'm trying to build a C# application which will detect the type of the mouse pointer.
You can define a specific cursor for each window class. Consult the documentation for the function RegisterClassEx and structure WNDCLASSEX
HTH.
The browser viewport is a simple window with hardly any standard events. A page is rendered by pixel and treated later as a bitmap. A browser builds an hierarchy of web page controls and display elements and keeps it in memory. Whenever mouse moves across the page, the browser algorithms search through this hierarchy to identify whether these particular coordinates belong to, say, a button or a link and then change the cursor to pointer. In short, it's what a browser engine is all about. Parse HTML to an hierarchy of controls, then parse CSS and update properties of these elements then render the controls under consideration of their properties to a viewport, then process user input and when required initiate a request. Browser engine also executes JavaScript code and performs manipulation on the document structure.
Remember also that FireFox exists for Linux as well in which case it would make no sense for browser developers to work with standard windows events. Some basic initialization code is definitely platform dependent but after the window is prepared and user input is forwarded through some abstraction layer to the core, then the browser engine leads the play with no concern for the underlying operating system and its event system whatsoever.

Drag and Drop to a hosted Browser control

I have a WinForms program written on .NET 2 which hosts a webbrowser control and renders asp.net pages from a known server.
I would like to be able to drag, say, a tree node from a treeview in my winforms app into a specific location in the hosted web page and have it trigger a javascript event there.
Currently, I can implement the IDocHostUIHandler interface and getting drag\drop events on the browser control, then call Navigate("javascript:fire_event(...)") on the control to execute a script on the page. However, I want this to work only when I drop data on a specific part of the page.
One solution, I suppose, would be to bite the bullet and write a custom browser plugin in the form of an activex control, embed that in the location I want to drop to and let that implement the needed drag\drop interfaces.
Would that work?
Is there a cleaner approach? Can I take advantage of the fact that the browser control is hosted in my app and provide some further level of interaction?
Take a look at the BrowserPlus project at Yahoo.
It looks like they have built a toolkit so that you don't have to do the gritty work of writing the browser plugin yourself.
If you can find out the on screen position of the part of the page you are interested in, you could compare this with the position of the mouse when you receive the drop event. I'm not sure how practical this is if you can get the info out of the DOM or whatnot.
As an alternative could you implement the mouse events on the bit of the page using javascript?

Categories

Resources