Submit web form on a GeckoWebBrowser? (GeckoFX)
Asked Answered
I

2

6

There is a properly way to submit a web form using GeckoFX library?

This is what I'm doing to fill a web form and submit the form, but...well, I'm not submitting, I'm just clicking on the last button of the page and I think that can't be the properly way to do it...

Framework's WebBrowser control has a method to submit a web form but I can't find any similar method to properly submit a web form on a GeckWebBrowser.

Dim doc = GeckoWebBrowser1.Document

doc.GetElementById("id_username").
    SetAttribute("value", CStr(MyUsername))

doc.GetElementById("id_password").
    SetAttribute("value", CStr(MyPassword))

doc.GetElementsByTagName("input").
    Last.Click()
Interlocutor answered 24/12, 2013 at 6:25 Comment(2)
have you tried this ? got any exception or error ?Strata
@xpertgun It works, does not throw any exception... but just I would like to know the properly way to submit a form like in a WebBrowser control with the WebBrowser1.Document.Forms(0).InvokeMember("Submit"), this is for generic usage, I can't hardcode the input value/name/tags or something else like in your answer, sorry, I just would like to know if exist something similar for generic use. Thankyou anyways!Interlocutor
T
4

The GeckoFormElement has a submit method.

So something like this:

(GetElementByTagName("form").First() as GeckoFormElement).submit()
Touchmenot answered 25/12, 2013 at 5:8 Comment(3)
Been searching for a couple of hours and this is the first thing that worked! ThanksAsymmetry
@Touchmenot sorry to arrive late, I don't know how to adapt the code... I'm working with a GeckoDocument not a GeckoFormElement, anyways I've tried to set a new GeckoFormElement but I don't know how to 'cause I can't Cast a GeckoDocument to a GeckoFormElement... I need a example to use your submit method with the code that I've posted in my question, thanks.Interlocutor
Ok Ive found the right casting by myself: CType(GeckoWebBrowser1.Document.GetElementsByTagName("Form").First, GeckoFormElement).Submit() or this which is the same GeckoWebBrowser1.Document.GetElementsByTagName("Form").Cast(Of GeckoFormElement).First.submit()Interlocutor
S
3

I can give example in c# :

If you know id value for input tags and login button , you can do this:

 GeckoInputElement username = new GeckoInputElement(geckoWebBrowser1.Document.GetElementById("Username_ID").DomObject);
 GeckoInputElement Passwd = new GeckoInputElement(geckoWebBrowser1.Document.GetElementById("passwd_ID").DomObject);
 GeckoInputElement Loginbutton = new GeckoInputElement(geckoWebBrowser1.Document.GetElementById("login_button_ID").DomObject);
 username.Value = "username";
 Passwd.Value = "password";
 Loginbutton.Click();

and if you know name of input tags, try this:

GeckoInputElement username = new GeckoInputElement(geckoWebBrowser1.Document.GetElementsByName("email")[0].DomObject);
GeckoInputElement password = new GeckoInputElement(geckoWebBrowser1.Document.GetElementsByName("pass")[0].DomObject);
GeckoInputElement login = new GeckoInputElement(geckoWebBrowser1.Document.GetElemntByName("login_name")[0].DomObject);
username.Value = "username";
password.Value = "password";
login.Click();

and if you dont know any id or name of input tags and have class name, try this,

GeckoNodeCollection nod = geckoWebBrowser1.Document.GetElementsByClassName("classname");
        foreach (GeckoNode node in nod)
        {
            if (NodeType.Element == node.NodeType)
            {

                try
                {
                    GeckoInputElement ele = (GeckoInputElement)node;
                    ele.Click();
                }
                catch (Exception ex)
                {
                    string ep = ex.ToString();
                    GeckoHtmlElement ele = (GeckoHtmlElement)no2;
                    ele.Click();
                }                    
            }
        }  
Strata answered 24/12, 2013 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.