.:: Tejas Shirodkar's Blog ::.

A Date with GeckoFx and InvokeMember(“click”);

by Tejas Shirodkar on Aug.04, 2009, under Development

A lot of people have been coming to my blog looking for information on GeckoFx mostly through a google search. I was successfully able to integrate GeckoFx into my crawler like app with some initial hiccups in place of the web browser 2.0 component. Although the web component exposes all functionality properly, it wasn’t enough for my needs. GeckoFx has alot of unexposed area’s and could do with a major upgrade. Anyhow I thought I would share some fixes I made or hacks I ended up with to fix some problems…

There were alot of fixes i had to do (to the code not GeckoFx) and I still haven’t been able to figure out how to get the content or the source code inside a frame or add security exceptions. One particular problem lots of people search for is the replacement for the InvokeMember(“click”) on a HtmlElement. The solution  i came across uses javascript. It’s basically like this -


geckoWebBrowser.Navigate( "javascript:void( document.getElementById( 'idOfElement' ).click() ) ");

where idOfElement is the element you want the click to be called on. Now in case the id is not available you can search for it by name or basically go through the whole file using javascript to get to your element.

I was also looking for a way to make changes to some settings like the about:config page on some extensive googling I came across Skybound.Gecko.GeckoPreferences you can change the user and default settings through this class at the time of initialization or at a later stage. So basically if you want to disable the warning for entering a secured site you would do -


Skybound.Gecko.GeckoPreferences.User["security.warn_entering_secure"] = false;

There were some other minor things here and there as well, but on those I’ll get to at a later time. In case there is somethings someone wants to know or knows of some better ways then kindly leave a comment or mail me, I’ll be glad to hear from you :)

Update [Sept' 1st 2009]

In case you need to use proxy to connect you can use the following code in your C# program. It can be used at any time in the point of execution and the next call to Navigate will use the new proxy configuration.


Skybound.Gecko.GeckoPreferences.User["network.proxy.http"] = "x.x.x.x";
Skybound.Gecko.GeckoPreferences.User["network.proxy.http_port"] = 8000;
Skybound.Gecko.GeckoPreferences.User["network.proxy.socks"] = "x.x.x.x";
Skybound.Gecko.GeckoPreferences.User["network.proxy.socks_port"] = 8000;
Skybound.Gecko.GeckoPreferences.User["network.proxy.socks_remote_dns"] = true;
Skybound.Gecko.GeckoPreferences.User["network.proxy.ssl"] = "x.x.x.x";
Skybound.Gecko.GeckoPreferences.User["network.proxy.ssl_port"] = 8000;
Skybound.Gecko.GeckoPreferences.User["network.proxy.type"] = 1;

You should replace x.x.x.x with the ip pf the proxy server, and 8000 with the port which is used for proxy in each case.

For network.proxy.type, the following values can be used :

0 – Direct connection, no proxy. (Default)

1 – Manual proxy configuration.

2 – Proxy auto-configuration (PAC).

4 – Auto-detect proxy settings.

5 – Use system proxy settings (Default in Linux).

:
21 comments for this entry:
  • aqi

    I want to write my program with geckofx in vs2005.but the dll import problem is confusing me.Would you please write a more detail article for beginners.

    I would appreciate it if you could help me.

  • http://www.tejasshirodkar.com Tejas Shirodkar

    Given the little amount and very fragment information available on GeckoFx, perhaps it might be a good idea to write one. Will post it once it’s done… Till then if you have a very specific problem and require a quick solution, you should probably mail me with the exact details. I’ll try to be of as much help as possible. :)

  • Gecko

    THX for the first tip!!

    i search and search but i’ve dont find anything like this! now my skript works, and now i think i have to lern javascript the next time…

  • bairog

    “… where idOfElement is the element you want the click to be called on. Now in case the id is not available you can search for it by name or basically go through the whole file using javascript to get to your element…”

    My javascript knowledge is poor so I’ve just tried construclion like this:

    geckoWebBrowser.Navigate( “javascript:void( var classes=document.getElementsByTagName(‘*’); for (i=0; i<classes.length; i++) {if (classes[i].className==star-1) classes[i].click() } )" )

    Of course I falied :) .
    So could you tell me how to write it correctly?
    Thank you.

  • http://www.tejasshirodkar.com Tejas Shirodkar

    To me there seem to be 2 main problems with the code you have provided.

    Firstly for getElementsByTagName you need to use the name of an HTML tag like ‘div’ or something, I don’t think ‘*’ is a valid argument.

    Secondly unless “star-1″ is the actual class name, the way you have used it in the comparison is incorrect, if it is then you should enclose the name in qoutes else replace it with the actual name of the class that you are searching for.

  • JD

    Hi,

    I was wondering if you’d the geckoWebBrowser.Navigate( “javascript:void( document.getElementById( ‘idOfElement’ ).click() ) “); trick on a link which has an onClick attribute defined?

    I was trying a basic scrapper on Facebook, and manually clicking the link, and programmatically clicking the link seem to produce difference results… it looks like the onClick function doesn’t get invoked properly when using the javascript to simulate the click.

    Have you seen this at all? Found a workaround?

  • http://www.tejasshirodkar.com Tejas Shirodkar

    I haven’t worked on a case like this but I could do a small test to check it out.

    In the mean while as a work around if it doesn’t work directly then I don’t think it would be hard to get either the onClick delegate or the script code from source and execute it before the click.

  • JD

    Tejas, thanks for the reply. It seems to be specific to Firefox/Gecko. Creating a test html page, and using a javascript function in the “onload” of the body to click a random link works fine in IE, but doesn’t do anything in Firefox. If that basic test case doesn’t work then I think GeckoFX is being crippled by the underlying Gecko code.

    Oh well. Thanks for your help.

    JD

  • VikFreeze

    This is an interesting post, i used the proxy settings as stated above and they work nicely, but the problem is that i need to set the proxy settings on a component basis.
    For example if i have 3 instances of geckoWebBrowser and i need each one of them to have its own proxy settings.
    The method above sets the proxy settings on for all 3 of the geckoWebBrowser instances.
    If u can give me some help on how to set the proxy settings on a component basis id really appreciate it

  • Imran Haider Ahsraf

    Please Help me with the problem of how to automatically give user name and password in Gecko Fx. I would be very grateful.

    Thanks in advance.

  • http://topsy.com/www.tejasshirodkar.com/blog/2009/08/date-with-geckofx-and-invokememberclick/?utm_source=pingback&utm_campaign=L2 Tweets that mention A Date with GeckoFx and InvokeMember(“click”); – .:: Tejas Shirodkar’s Blog ::. — Topsy.com

    [...] This post was mentioned on Twitter by Javascript
    News. Javascript News said: A Date with GeckoFx and
    InvokeMember(“click”); – .:: Tejas Shirodkar’s Blog ::.
    http://ow.ly/1aMOE5 [...]

  • surekha

    I need to automatically/programatically click on ok button in the confirm box of javascript that pops up while navigating thru a url using geckowebbrowser.

    Please suggest .

    Thanks in advance

  • Surekha Goel

    I would like to know how to enter secure sites through GeckoWebBrowser. I am using GeckoWebBrowser to browse a https site, then it give “This Connection is Untrusted” error.

    I have added security certificate in Firefox and the site opens from there, but the site is not opening from a .NET window prg using GeckoWebBrowser.

  • http://tejasshirodkar.com Tejas Shirodkar

    TLDR Version: You will have to expose the SSL code from XULRunner or disable the warnings. Someone did it and shared the 2 lines of code which enable that at –

    http://www.geckofx.org/viewtopic.php?pid=3549

    When I last used GeckoFx about a year and a half ago it did not support saving the security certificates and the only way to get around it was to extend/expose the functionality into GeckoFX from XULRunner yourself.

    As it turns out it still is the case today. However, the GeckoFX forums are usually a good place to get information, even though it is fragmented.

    Luckily someone took the task on himself a year back and posted his results and changes on to the forums. You can implement the same changes which will save you a lot of time. The post is at -

    http://www.geckofx.org/viewtopic.php?pid=3549

  • Surekha Goel

    I copied the file cert_override.txt file from Firefox folder to C:Documents and SettingsLocal SettingsApplication DataGeckofx10902DefaultProfile folder and it works fine.

  • http://tejasshirodkar.com Tejas Shirodkar

    Thats great news and is easier than having to expose the save functionality through your own component!

    Thanks a lot for sharing, will be helpful for others seeking a solution to the same problem!

  • Nishant

    Thanks for help i am now able to clicks on element but one problem i come across with “https://adwords.google.com/o/Targeting/Explorer?__u=1000000000&__c=1000000000&ideaRequestType=KEYWORD_IDEAS#search.none” site and not able to clicks on “NEXT” button for paging please help me if someone has a good knowledge of javascript. I tried below script but its not working:

    function fireEvent(obj, evt) {

    var fireOnThis = obj;

    if (document.createEvent) {

    var evObj = document.createEvent(“MouseEvents”);

    evObj.initEvent(evt, true, false);

    fireOnThis.dispatchEvent(evObj);

    } else if (document.createEventObject) {

    fireOnThis.fireEvent(“on” + evt);

    }

    }

    fireEvent(document.getElementById(“gwt-debug-aw-paging-next-content”), “click”);

    Thanks in advance.

  • http://tejasshirodkar.com Tejas Shirodkar

    Have you tried to make it run straight up without the event code? The navigate portion is important to execute the javascript code, at what point in the execution of the program are you trying to fire your event?

  • Farooq Pathan

    How can be click anchor tags and the elements without id or name, for example 

    Click

    Looking forward for some help.

  • Stokarchuk

    Tejas u even can’t imagine how your post help me.
     10x alot .   

  • http://tejasshirodkar.com Tejas Shirodkar

    It’s great to know that something I wrote so long ago is still helping people!

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!