1 回答

TA貢獻1898條經驗 獲得超8個贊
我在 WPF 應用程序中玩弄并重新創建了您的場景。
我使用DocumentCompleted事件 讓它工作
在瀏覽器完成解析新頁面并更新 Document 屬性后發生。
我在導航之前訂閱事件偵聽器,并在調用處理程序后將其刪除。
然后,我調用 的第一個元素form來提交搜索。
(_browser.Document.GetElementsByTagName("form").First() as GeckoFormElement).submit();
完整代碼示例:WPF 應用程序
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 和其他動態內容)被多次觸發。
- 1 回答
- 0 關注
- 183 瀏覽
添加回答
舉報