1

I'm tiring to emulate the google search with gecko web browser. so far i have able to go to the google page and then search some thing like this:

    geckoWebBrowser1.Navigate("https://www.google.com/");
    await Task.Run(() => CheckDocumentLoaded());

    var page = geckoWebBrowser1.Document.GetElementById("lst-ib");
    (page as GeckoHtmlElement).Focus();
    (page as GeckoInputElement).Value = "something";

now i simply want to click on the search button. so i added this to the first part:

    var button = new GeckoButtonElement(geckoWebBrowser1.Document.GetElementById("mKlEF").DomObject);
    button.Click();

but funny things happens. if i run this code after the first part nothing will happens. but if i created a button and put the code on it it works just fine.

private void Button1_Click(object sender, EventArgs e)
{
    var button = new GeckoButtonElement(geckoWebBrowser1.Document.GetElementById("mKlEF").DomObject);
    button.Click();

    return;
}

but i have to click on button manually in order to make it work. its really confusing. i have no idea what causes this!!

NOTE:

  1. you have to use this user agent if you want to the code works: (Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko)

  2. i don't want to use the SendKeys.Send("{ENTER}").

  3. if i press the button programmatically its not work either.

4
  • 1
    If "mKlEF" is a button then you should be able to just cast the return value from GetElementById to GeckoButtonElement, rather than creating another one. Doubt its the cause of your problem though. Commented Aug 31, 2018 at 21:47
  • 1
    Some guesses: 1. don't focus lst-ib element. 2. focus the button. Commented Aug 31, 2018 at 21:51
  • dear @Tom. i tried it but it didn't work. Commented Sep 1, 2018 at 3:56
  • 1
    I believe this is caused by the AJAX nature of the Google start page. Commented Sep 3, 2018 at 22:54

1 Answer 1

2

I played around and recreated your scenario in a WPF app.

I got it working using the DocumentCompleted event that

occurs after the browser has finished parsing a new page and updated the Document property.

I subscribe to the event listener before navigation and remove it once the handler is invoked.

Then, I call the first element of the form to submit the search.

(_browser.Document.GetElementsByTagName("form").First() as GeckoFormElement).submit();

Full code sample: WPF app

using Gecko;
using Gecko.DOM;
using System.Windows;
using System.Windows.Forms.Integration;
using System.Linq;    
namespace GeckoWpf {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            Gecko.Xpcom.Initialize("Firefox");
        } 

        void browser_DocumentCompleted(object sender, System.EventArgs e) {
             //unsubscribe
            _browser.DocumentCompleted -= browser_DocumentCompleted;

            XPathResult xpathResult = _browser.Document.EvaluateXPath("//div/input");
            var foundNodes = xpathResult.GetNodes();
            foreach (var node in foundNodes) {
                GeckoInputElement txtbox = new GeckoInputElement(node.DomObject);
                txtbox.Value = "Mona Lisa"; //add the search term
            }    
            (_browser.Document.GetElementsByTagName("form").First() as GeckoFormElement).submit();
        }

        WindowsFormsHost _host = new WindowsFormsHost();
        GeckoWebBrowser _browser = new GeckoWebBrowser();    
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            _browser.DocumentCompleted += browser_DocumentCompleted;
            _host.Child = _browser;    GridWeb.Children.Add(_host);    
            _browser.Navigate("https://www.google.com/");
        }
    }
}

Note: This approach may not work on all pages since DocumentComplete may get fired multiple times for various reasons (e.g. i/frames, AJAX and other dynamic stuff).

PS: Nonetheless, your endeavor may or may not be legal.
You may want to consider using Google's custom search API or alternatives like SerpApi instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.