C#中的HtmlAgilityPack - 找不到ID的Div

时间:2016-08-11 10:04:15

标签: c# html-agility-pack

我有当前的代码,可以提取出正常的版本号,因为它有一个已定义的div id:

 ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
                        HtmlWeb web = new HtmlWeb();
                        HtmlDocument doc = web.Load(Url);
                        HtmlNode rateNode = doc.DocumentNode.SelectSingleNode("//div[@id='version']");
                        string VersionNumber = rateNode.InnerText;

我也试图从HTML源代码中获取第二个版本号,但是它没有div ID。我尝试使用:

HtmlNode h2Node = doc.DocumentNode.ChildNodes["display: none"];

这也没有用,所以我怎样才能找到这个版本号,来自下面的代码片段:

<div style="display: none">16.2.6011.18020|Jun-16-16 02:00:44</div>

HTML来源:

    <script type="text/x-kendo-template" id="logonDisabledWindowTemplate">
        <div>
            <div style="padding-top: 15px;padding-left: 10px;">
                Login denied as the user is inactivated.
            </div>
            <div>
                <div class="k-button blueButtonSimple" id="btnLogonDisabledOk">OK</div>
            </div>
        </div>
    </script>

    <html>

    <div id="version" class="versionLogin"> Version:  16.2.6011</div>
    <script type="text/x-kendo-template" id="logonDisabledWindowTemplate">
    <div>
        <div style="padding-top: 15px;padding-left: 10px;">
            Login denied as the user is inactivated.
        </div>
        <div>
            <div class="k-button blueButtonSimple" id="btnLogonDisabledOk">OK</div>
        </div>
    </div>
</script>    
    <script type="text/javascript">
        Localization.setLocalizedString("loginAuthError", "The username or password you entered is incorrect. Please try again.");
        Localization.setLocalizedString("InvalidCharacters", "Leading spaces, *, ; ,  &gt; , &lt; , = and &quot; characters cannot be used in text values.");
        Localization.setLocalizedString("Profile_RequiredField", "Required field");
        Localization.setLocalizedStrings({
            changePwdOldPwdDidnotMatch: "Old password did not match",
            PasswordSameAsOld: "New password cannot be same as old password.",
            changePwdUnknownUser: "Unknown user",
            changePwdFailed: "Change password failed",
            MSG_12000: "Password should contain at least {0} characters. ",
            MSG_12001: "Password should contain uppercase letters, lowercase letters, numbers and symbols. ",
            MSG_12002: "Password should not be one of the last {0} passwords used. ",
            MSG_12003: "Unable to validate password. ",
            MSG_12004: "The password entered must be at least 6 characters and not longer than 32 characters. ",
            MSG_12005: "You have been automatically logged out for security reasons as the same user has logged on to another session. ",
            MSG_12006: "Password should contain letters, numbers and symbols. ",
            PasswordMismatch: "The passwords do not match. Please try again.",
            UserAccountLockedMsgPart1: "Your account has been locked for security reasons, as there have been at least {0} invalid attempts to log in to your account. ",
            UserAccountLockedMsgPart2: "Lock out is in force for {0} minutes. If you do not want to wait, {1}.",
            DoNotWantToWaitInstructionForNormalUser: "you can reset your password by clicking the &quot;I forgot my password&quot; link",
            DoNotWantToWaitInstructionForSysadmin: "please contact your regional admin to reset password"
        });
    </script>

    <div style="display: none">16.2.6011.18020|Jun-16-16 02:00:44</div>

    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

您也可以通过属性名称获取它。

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(Url);
var data = doc.DocumentNode.Descendants()
               .Where(n => n.GetAttributeValue("style",   "").Equals("display: none")).FirstOrDefault();
string version = String.Empty;
if (data!= null)
{
  version = data.InnerText;
}