简体   繁体   English

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

[英]How to run GeckoFx “GetElementById” funtion in C#?

I've started a new project recently and it was working properly with the default Visual Studio 2015 "webBrowser". 我最近启动了一个新项目,并且使用默认的Visual Studio 2015“ webBrowser”可以正常工作。 Unfortunately the default browser doesn't support some web elements that I need it to run. 不幸的是,默认浏览器不支持我需要运行的某些Web元素。 Therefor I decided to look for a Browser alternative that could run this elements. 因此,我决定寻找一种可以运行此元素的浏览器替代产品。

After doing some research I decided to go with GeckoFx, mainly because I read that it's command functions work pretty much like the original webBrowser (commands like; GetElementById). 经过研究后,我决定使用GeckoFx,主要是因为我了解到它的命令功能与原始的webBrowser(命令类似; GetElementById)非常相似。

I'm trying to create a program that logs me in to "login.live.com" automatically. 我正在尝试创建一个程序,该程序自动将我登录到“ login.live.com”。 I'm not sure why but when it runs the code, the "password" value entry seams to work, but the email one does not. 我不知道为什么,但是当它运行代码时,“ password”值输入接缝可以正常工作,但是电子邮件不起作用。 Please help me figure out why! 请帮我找出原因!

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

This is the form design: 这是表单设计:

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

If anyone knows of a better way to do this please let me know PLEASE HELP!! 如果有人知道更好的方法,请告诉我!

I was able to replicate the behavior you witnessed. 我能够复制您目击的行为。 I believe it is because the input field type is "email" which triggers validation. 我相信这是因为输入字段类型为“电子邮件”,这会触发验证。 I was able to populate the field by using the GeckoInputElement type from the Gecko.DOM module. 我能够通过使用Gecko.DOM模块中的GeckoInputElement类型来填充字段。

The following code works for me: 以下代码对我有用:

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