[英]How to run GeckoFx “GetElementById” funtion in C#?
我最近啟動了一個新項目,並且使用默認的Visual Studio 2015“ webBrowser”可以正常工作。 不幸的是,默認瀏覽器不支持我需要運行的某些Web元素。 因此,我決定尋找一種可以運行此元素的瀏覽器替代產品。
經過研究后,我決定使用GeckoFx,主要是因為我了解到它的命令功能與原始的webBrowser(命令類似; GetElementById)非常相似。
我正在嘗試創建一個程序,該程序自動將我登錄到“ login.live.com”。 我不知道為什么,但是當它運行代碼時,“ password”值輸入接縫可以正常工作,但是電子郵件不起作用。 請幫我找出原因!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace XLP_2_11_16__2_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//This is not the actual folder location. Part of it was suppstituded by "..." for safety reasons.
Gecko.Xpcom.Initialize(@"C:\...\bin\Debug\xulrunner");
}
private void Form1_Load(object sender, EventArgs e)
{
geckoWebBrowser1.Navigate("https://login.live.com");
}
private void button1_Click(object sender, EventArgs e)
{
//Actual email and password was substituted for safety reasons.
string email = "myemail@email.com";
string pass = "mypassword";
geckoWebBrowser1.Document.GetElementById("i0116").SetAttribute("value", email);
geckoWebBrowser1.Document.GetElementById("i0118").SetAttribute("value", pass);
geckoWebBrowser1.Navigate("javascript:void(document.forms[0].submit())");
}
private void geckoWebBrowser1_Click(object sender, EventArgs e)
{
}
private void geckoWebBrowser1_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
{
}
public void Delayed(int delay, Action action)
{
Timer timer = new Timer();
timer.Interval = delay;
timer.Tick += (s, e) => {
action();
timer.Stop();
};
timer.Start();
}
}
}
這是表單設計:
如果有人知道更好的方法,請告訴我!
我能夠復制您目擊的行為。 我相信這是因為輸入字段類型為“電子郵件”,這會觸發驗證。 我能夠通過使用Gecko.DOM模塊中的GeckoInputElement類型來填充字段。
以下代碼對我有用:
var emailField = new Gecko.DOM.GeckoInputElement(geckoWebBrowser1.Document.GetHtmlElementById("i0116").DomObject);
emailField.Value = @"test@test.com";
var passwordField = new Gecko.DOM.GeckoInputElement(geckoWebBrowser1.Document.GetHtmlElementById("i0118").DomObject);
passwordField.Value = @"test_password";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.