我在一个WPF应用程序中重新创建了你的场景。
DocumentCompleted
事件
我在导航之前订阅事件侦听器,并在调用处理程序后将其删除。
然后,我调用
form
提交搜索。
(_browser.Document.GetElementsByTagName("form").First() as GeckoFormElement).submit();
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/");
}
}
}
注意:这种方法可能不适用于所有页面,因为
DocumentComplete
可能会因为各种原因被多次解雇(例如i/frames、AJAX和其他动态内容)。
附言:尽管如此,你的努力可能会失败,也可能会失败
not be legal
.
你可以考虑使用
Google's custom search API
或者像这样的替代品
SerpApi