我如何从网站获得一个类元素值?

时间:2015-12-23 08:57:15

标签: c# html .net winforms

我有这个元素

<span class="validationMsg">
    <span id="TapuzLogin1_ctl01_UserNameRequired" title="יש להקליד שם משתמש" style="color:Red;display:none;">* יש להקליד שם משתמש<br>
    </span>
    <span id="TapuzLogin1_ctl01_PasswordRequired" title="יש להקליד סיסמה" style="color:Red;display:none;">* יש להקליד סיסמה<br>
    </span>שם המשתמש או הסיסמה אינם נכונים
</span>

该类是“validationMsg” 我需要获取字符串的值在底部:

שםהמשתמשאוהסיסמהאינםנכונים

这是希伯来文。

我可以通过Id

获取和使用
private void button1_Click(object sender, EventArgs e)
{
   navigate("mysite.net");
   webBrowser1.Document.GetElementById("UserName").InnerText = textBox1.Text.ToString();
   webBrowser1.Document.GetElementById("Password").InnerText = textBox2.Text.ToString();
   webBrowser1.Document.GetElementById("LoginButton").InvokeMember("click");
}

但是如何获取希伯来语中此字符串的类元素值表示我是否登录了。 我想检查一下,如果这个元素包含这个字符串,那么登录就错了。

这是我在form1中的完整代码,用于登录。 如果我输入正确的用户名和密码,我可以登录。 但是,如何指出是否输错了密码或用户名?

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;
using mshtml;
using HtmlAgilityPack;

namespace WebSite_Login_And_Browsing
{
    public partial class LogIn : Form
    {
        IHTMLDocument2 doc;
        string pagecontent;

        public LogIn()
        {
            InitializeComponent();

            webBrowser1.ScriptErrorsSuppressed = true;
        }

        public void ihtml(HtmlAgilityPack.HtmlDocument dom)
        {
            try
            {
                pagecontent = "";
                doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
                pagecontent = doc.body.innerHTML;
                dom.LoadHtml(pagecontent);
            }
            catch (Exception)
            {
                dom.LoadHtml(webBrowser1.DocumentText.ToString());
            }

        }

        public void navigate(string url)
        {
            webBrowser1.Navigate(url);
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            navigate("http://www.tapuz.co.il/Common/SignInPage.aspx?backUrl=http://www.tapuz.co.il/Common/SignIn.aspx@loginDone=1");

            webBrowser1.Document.GetElementById("UserName").InnerText = textBox1.Text.ToString();       webBrowser1.Document.GetElementById("Password").InnerText = textBox2.Text.ToString();          webBrowser1.Document.GetElementById("LoginButton").InvokeMember("click");
        }
    }
}

我在button1中的代码点击按钮事件

private void button1_Click(object sender, EventArgs e)
            {
navigate("http://www.tapuz.co.il/Common/SignInPage.aspx?backUrl=http://www.tapuz.co.il/Common/SignIn.aspx@loginDone=1");         webBrowser1.Document.GetElementById("TapuzLogin1_ctl01_UserName").InnerText = textBox1.Text.ToString();         webBrowser1.Document.GetElementById("TapuzLogin1_ctl01_Password").InnerText = textBox2.Text.ToString();       webBrowser1.Document.GetElementById("TapuzLogin1_ctl01_LoginButton").InvokeMember("click");
            }

文件已完成的活动

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var items = webBrowser1.Document.GetElementsByTagName("span");
            foreach (HtmlElement item in items)
            {
                if (item.GetAttribute("className") == "validationMsg")
                {
                    logingResults = item.InnerText;
                    if (logingResults.Contains("שם המשתמש או הסיסמה אינם נכונים"))
                        break;
                }
            }
        }

3 个答案:

答案 0 :(得分:1)

var items = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement item in items)
{
    if (item.GetAttribute("className") == "validationMsg")
    {
        // Now you can check the item's value by comparing item.InnerText
    }
}

答案 1 :(得分:1)

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;
using mshtml;
using HtmlAgilityPack;

namespace WebSite_Login_And_Browsing
{
    public partial class LogIn : Form
    {
        public LogIn()
        {
            InitializeComponent();

            webBrowser1.ScriptErrorsSuppressed = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.DocumentCompleted += process1;
            webBrowser1.Navigate("http://www.tapuz.co.il/Common/SignInPage.aspx?backUrl=http://www.tapuz.co.il/Common/SignIn.aspx@loginDone=1");
        }

        private void process1(object sender, WebBrowserDocumentCompletedEventArgs args)
        {
            webBrowser1.DocumentCompleted -= process1;
            webBrowser1.DocumentCompleted += process2;

            try
            {
                webBrowser1.Document.GetElementById("UserName").InnerText = textBox1.Text.ToString();
                webBrowser1.Document.GetElementById("Password").InnerText = textBox2.Text.ToString();
                webBrowser1.Document.GetElementById("LoginButton").InvokeMember("click");
            }
            catch
            {
                webBrowser1.DocumentCompleted -= process2;
            }
        }

        private void process2(object sender, WebBrowserDocumentCompletedEventArgs args)
        {
            webBrowser1.DocumentCompleted -= process2;

            var items = webBrowser1.Document.GetElementsByTagName("span");
            foreach (HtmlElement item in items)
            {
                if (item.GetAttribute("className") == "validationMsg")
                {
                    if (item.InnerText.Contains("שם המשתמש או הסיסמה אינם נכונים"))
                    {
                        MessageBox.Show("State 1");
                        return;
                    }
                }
            }

            MessageBox.Show("State 2");
        }
    }
}

答案 2 :(得分:0)

不确定您是否愿意使用javascript来获取文字。希望此代码段可以帮助您

document.getElementsByClassName('validationMsg')[0].innerText

WORKING DEMO

如果我错过了任何一点,请道歉