繁体   English   中英

如何在C#中运行GeckoFx“ GetElementById”功能?

[英]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();
        }
    }
}

这是表单设计:

http://i.imgur.com/O211Mgu.png?1

如果有人知道更好的方法,请告诉我!

我能够复制您目击的行为。 我相信这是因为输入字段类型为“电子邮件”,这会触发验证。 我能够通过使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM