webBrowser元素未在修订后的代码中填充

时间:2015-11-24 18:05:52

标签: c#

我正在重写我的程序的很大一部分,并且遇到了对我来说根本没有的东西。

第一段代码完美无缺。 Web页面将在webBrowser控件中打开。填充用户名字段以及密码字段。按钮点击没有任何问题。

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TimeClockEntry2
{
    public partial class PNow : Form
    {
        int linkcount = 0;
        string userName;
        string passWord;

        public PNow()
        {
            InitializeComponent();
//            ControlBox = false;  // This line hides the Minimize, Maximize, and Close buttons
            webBrowser1.Navigate("https://ew23.ultipro.com/login.aspx");
        }

        private void PNow_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            userName = Properties.Settings.Default.userName;
            passWord = Properties.Settings.Default.passWord;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            linkcount++;

            if (linkcount == 1)
            {
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_UserName").SetAttribute("Value", userName);
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_Password").SetAttribute("value", passWord);
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_LoginButton").InvokeMember("click");
            }

        }
    }
}

第二个代码块打开网页然后停止。没有其他事情发生。

任何想法都很棒,谢谢。

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TimeClockEntry3
{
    public partial class PNow : Form
    {
        int linkcount = 0;
        string userName;
        string passWord;

        public PNow()
        {
            InitializeComponent();
//            ControlBox = false;  // This line hides the Minimize, Maximize, and Close buttons
            webBrowser1.Navigate("https://ew23.ultipro.com/login.aspx");
        }

        private void PNow_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            userName = Properties.Settings.Default.USERNAME;
            passWord = Properties.Settings.Default.PASSWORD;

            //MessageBox.Show(userName);
            //MessageBox.Show(passWord);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            linkcount++;

            if (linkcount == 1)
            {
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_UserName").SetAttribute("Value", userName);
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_Password").SetAttribute("value", passWord);
                webBrowser1.Document.GetElementById("ctl00_Content_Login1_LoginButton").InvokeMember("click");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

没关系......

我需要在表单属性中关联DocumentCompleted事件。 谢谢。