GeckoFX

Skybound

Skybound Software is looking to hire experienced C# developers to work on Stylizer and other forthcoming Skybound products. Click here for details.

GeckoFX is an open-source component which makes it easy to embed Mozilla Gecko (Firefox) into any .NET Windows Forms application. Written in clean, fully commented C#, GeckoFX is the perfect replacement for the default Internet Explorer-based WebBrowser control. Download GeckoFX @ Google Code >

GeckoFX is made by Skybound Software, authors of Stylizer—the professional CSS editor with a strong focus on visual control, error-free workflow, and productivity. Learn About Stylizer >

GeckoFX Forum

Discuss GeckoFX, the Open-Source Gecko Control for .NET

You are not logged in.

Link Click

#1 February 01, 2010 8:26 AM

delopero
Member
Registered: January 07, 2010
Posts: 10
Website

Link Click

Does anybody know how to simulate click on the link from my code?

Offline

 

#2 February 01, 2010 11:56 AM

Cristian
Member
Registered: January 16, 2010
Posts: 17

Re: Link Click

The key is in this post:
http://geckofx.org/viewtopic.php?id=578

You have to add the contents of the rar commented to the source code of GeckoFX.
From that moment on you can start using the DOMSelector to get objects of various types that will allow you to do things like clicks.

Offline

 

#3 February 01, 2010 12:23 PM

delopero
Member
Registered: January 07, 2010
Posts: 10
Website

Re: Link Click

Unfortunately classes HTMLAnchorElement and HTMLLinkElement do not support Click methods.
Also as far as I know links in FireFox does not support click method.
Is this any way to simulate it from the code?

Offline

 

#4 February 01, 2010 12:27 PM

Cristian
Member
Registered: January 16, 2010
Posts: 17

Re: Link Click

Why not using the navigate function?
If it has javascript you might be also able to emulate it.

Offline

 

#5 February 01, 2010 12:38 PM

delopero
Member
Registered: January 07, 2010
Posts: 10
Website

Re: Link Click

Yes this is the variant, but in this case we have too much cases:
1) yes it can be javascript (not a problem)
2) but this can also be a link in a frame, and i should navigate the exact frame to the href of link, how can i do it? Is there any way to navigate the exact frame (not the whole browser)?

Offline

 

#6 February 03, 2010 8:43 AM

Sire
Member
Registered: January 14, 2010
Posts: 14

Re: Link Click

Use for example:

Code:

browser.Navigate("javascript:void( window.frames[2].document.getElementById( 'buttonID' ).click() ) ");

Offline

 

#7 February 04, 2010 11:34 AM

delopero
Member
Registered: January 07, 2010
Posts: 10
Website

Re: Link Click

you do not have access from javascript to frames from domain differs from domain of the main document.

Last edited by delopero (February 04, 2010 11:35 AM)

Offline

 

#8 February 04, 2010 11:48 AM

Sire
Member
Registered: January 14, 2010
Posts: 14

Re: Link Click

This is because of the security risks of allowing Cross Site Scripting

Offline

 

#9 February 26, 2010 1:04 PM

ZFedeZ
Member
Registered: February 26, 2010
Posts: 1

Re: Link Click

Hi everyone,

I've been struggling and making research about this for a while now.
I want to simulate a link click in Firefox using Javascript. Directly navigating to the address is not enough in my case.
I try a specific code in a Javascript console and it works, but when I use the same code with GeckoFX the link is not clicked.

I looked at the mentioned post about the DOMSelector (DOM.rar in http://geckofx.org/viewtopic.php?id=578) but I don't know how to use it.
I guess I need to get Gecko FX Source, add those files in some place and rebuild it?

Here goes the code I'm trying on a Visual Studio 2008 C# Windows Form:

Code:

            string js = @"javascript:
                l = document.getElementById('link'); 
                alert('link = ' + l);
                function fireEvent(obj,evt){
                    alert('fireEvent');
                    var fireOnThis = obj;
                    if( document.createEvent ) {
                           alert('createEvent');
                        var evObj = document.createEvent('MouseEvents');
                        evObj.initEvent( evt, true, false );
                        fireOnThis.dispatchEvent(evObj);
                    } else if( document.createEventObject ) {
                        fireOnThis.fireEvent('on'+evt);
                    }
                }
                fireEvent(l, 'click');
            ";

            geckoWebBrowser1.Navigate(js);

The link element is obtained correctly but the alert('fireEvent') never gets triggered.

Is my approach correct or should I try other way?
Does Firefox somehow prevents clicking a link programatically?

Any help on this would be appreciated.

Thanks in advance.

Regards,

Fede

Offline

 

#10 March 11, 2010 8:08 AM

delopero
Member
Registered: January 07, 2010
Posts: 10
Website

Re: Link Click

Gecko does not have event click for links, this  will work only for buttons ((
It seems to me that we should simulate clicks by calling WInAPI SendMessage or PostMessage.

Offline

 

#11 March 31, 2010 6:45 PM

hex
Member
Registered: March 13, 2010
Posts: 13

Re: Link Click

Anyone managed to do that ?

I am making scraper and for now it is navigating to javascript:blabla links but for some site that is not working (no href attributes in links at all)

AFAIK we can get XY cordinates of every node on the page, anyone know how to send winapi click to that location ?

Offline

 

#12 March 31, 2010 9:23 PM

softwerx
Member
From: Goshen, IN, US
Registered: September 04, 2008
Posts: 466
Website

Re: Link Click

hex wrote:

Anyone managed to do that ?

I am making scraper and for now it is navigating to javascript:blabla links but for some site that is not working (no href attributes in links at all)

AFAIK we can get XY cordinates of every node on the page, anyone know how to send winapi click to that location ?

Note:
Since links in a web page are not *controls* (in that they do not have a handle) you can't use SendMessage with WM_MOUSEDOWN/WM_MOUSEUP to them. As far as the X, Y values are concerned.. are you sure you can get the correct values? If not I have some VB.Net code laying around somewhere that can get the correct location - using GeckoFx - if you need it.

But, as far as sending a windows message.. you're kinda out of luck with that as the whole page would be considered as a single control - the GeckoFx control that is. The WinApi has no discerning api's that you can use for this purpose. (you' can trust me on this as I've taken the time to disassemble damn near every assembly in the Windows installation for the project I'm working on now ;) )

I know this isn't a whole lotta help but, I hope this helps you out in one way or another.

Best Regards,
Softwerx

Last edited by softwerx (March 31, 2010 9:25 PM)

Offline

 

#13 March 31, 2010 9:48 PM

hex
Member
Registered: March 13, 2010
Posts: 13

Re: Link Click

For cordinates I tought I saw someware properties that return XY relative to geckoWebBrowser control, now i can find only
GeckoElement.OffsetLeft
GeckoElement.OffsetTop
They as I see are relative to their DOM parent. If we sum all XY values of parents all the way up to BODY element we may get right result.


For sending WM_MOUSEDOWN off course we send it to geckoWebBrowser but we also send coordinates as parameters right ?

Offline

 

#14 March 31, 2010 9:51 PM

hex
Member
Registered: March 13, 2010
Posts: 13

Re: Link Click

I goggled some code
http://pastebin.com/m41fdaab1
Don't know what is it doing (comments are in German) but it looks it is calculating element position just as I thought so I guess it works...


Code:

#
                    //findet absolute Position des Videocontainers
#
                    //indem er sich bis zum body element hochhangelt
#
                    //und von jedem �bergeordneten element top und left aufaddiert
#
                    int xParameter = 0;
#
                    int yParameter = 0;
#
 
#
                    GeckoElement node = objectCollection[i];
#
 
#
                    while (node.TagName != "BODY")
#
                    {
#
                        //MessageBox.Show(node.TagName);
#
                        xParameter += node.OffsetLeft;
#
                        yParameter += node.OffsetTop;
#
                        node = node.Parent; //ebene h�her gehen
#
                    }

Offline

 

#15 March 31, 2010 10:31 PM

softwerx
Member
From: Goshen, IN, US
Registered: September 04, 2008
Posts: 466
Website

Re: Link Click

I'll pull up my vb code and post it tomorrow. It isn't the same as the German code you had posted but it is definitely accurate as I had used it in my old GeckoFx project to place a drop down under certain web controls.

Now that I think about it a bit more, you may be able to pull off the wm_mousedown/mouseup msg trick using the coordinates but, I haven't tested this myself. I'm not positive but, the DOM files that I had posted (originally by M1Labbe, I believe) may be useful in your endeavor. (You'll have to use the forum search to find them but, I'm sure I posted them as attachments to several of my posts). In any case, good luck on your project.

Best Regards,
Softwerx

EDIT: Unfortunately, I'm unable to find the project in which I have the code. If I run across it some time soon I'll be sure to post it for you.

Last edited by softwerx (April 01, 2010 9:41 PM)

Offline

 

#16 April 02, 2010 9:03 AM

hex
Member
Registered: March 13, 2010
Posts: 13

Re: Link Click

I think I made it....
Sending message to GWB control doesn't work becouse there is mozilla window inside and click is not routed to it.
I used FindWindowEx to find handle of class MozillaContentWindowClass in GWB and then another FindWindowEx to find MozillaWindowClass inside MozillaContentWindowClass.

When I send WM_LBUTTONDOWN, WM_LBUTTONUP to that MozillaWindowClass  it WORKS :)


I will post code later...

Offline

 

#17 April 02, 2010 11:46 AM

softwerx
Member
From: Goshen, IN, US
Registered: September 04, 2008
Posts: 466
Website

Re: Link Click

Glad to hear you got it working. I'll still post my code for getting element coordinates on a page when I run across it.

Best Regards,
Softwerx

Offline

 

#18 April 03, 2010 7:35 PM

hex
Member
Registered: March 13, 2010
Posts: 13

Re: Link Click

Is there a method to scroll gecko window exactly x pixels ?
I need a way to scroll so I can click on a element that is not visible..

Offline

 

Board footer