HtmlAgilityPack:如何检查元素是否可见? [英] HtmlAgilityPack: How to check if an element is visible?

查看:64
本文介绍了HtmlAgilityPack:如何检查元素是否可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一些HTML解析,并且正在使用 HtmlAgilityPack ,并且我正在尝试检查是否有节点元素如果html是在浏览器中呈现的,则将可见.

I am doing some HTML parsing and I am using HtmlAgilityPack and I am trying to check if a node element would be visible if the html was rendered in a browser.

可见,我可能对检查displayvisibility样式值感到满意. (除非还有其他我要担心的事情?).

By visible, I am probably content with checking the display and visibility style values. (unless there is something additional I should worry about?).

那么,我该怎么做?是否有简单的构建方式?我可以使用一些XPath魔术吗? (目前我对XPath的了解不多.)

So, how can I do this? Is there a build in easy way? Can I use some XPath magic? (I don't have too much knowledge of XPath at the moment).

我曾考虑过手动解析样式值,但宁愿将其保存为万不得已.还是这是我唯一的选择?

I have thought about manually parsing the style value, but would rather save this as a last resort. Or is this my only option?

仅供参考,我正在使用的对象是这样的:

Just for reference, the object I am working with is something like this:

HtmlAgilityPack.HtmlNode node = GetNode();

推荐答案

好,所以我设法做到了,至少出于我的需要.但是请注意,正如其他评论所提到的那样,这不允许您检查元素是否对最终用户可见(在屏幕上).

OK, so I have managed to do this, at least for my needs. Please be warned however, as other comments have spoke of, this doesn't allow you to check if an element will be visible (on screen) for the end user.

我简单采取的方法检查了一些基本规则:如果元素的样式属性包含display:nonevisibility:hidden,或者祖先元素具有相同的样式规则,则该元素不可见".

The approach I have taken simple checks some basic rules: An element is "not visible" if the style attribute for the element contains display:none or visibility:hidden, OR an ancestor element has the same style rules.

请记住,这是我的代码为我完成的工作:

With that in mind, here is my code that does the job for me:

private static bool IsNodeVisible(HtmlAgilityPack.HtmlNode node)
{
    var attribute = node.Attributes["style"];

    bool thisVisible = false;

    if (attribute == null || CheckStyleVisibility(attribute.Value))
        thisVisible = true;

    if (thisVisible && node.ParentNode != null)
        return IsNodeVisible(node.ParentNode);

    return thisVisible;
}

private static bool CheckStyleVisibility(string style)
{
    if (string.IsNullOrWhiteSpace(style))
        return true;

    var keys = ParseHtmlStyleString(style);

    if (keys.Keys.Contains("display"))
    {
        string display = keys["display"];
        if (display != null && display == "none")
            return false;
    }

    if (keys.Keys.Contains("visibility"))
    {
        string visibility = keys["visibility"];
        if (visibility != null && visibility == "hidden")
            return false;
    }

    return true;
}

public static Dictionary<string, string> ParseHtmlStyleString(string style)
{
    Dictionary<string, string> result = new Dictionary<string, string>();

    style = style.Replace(" ", "").ToLowerInvariant();

    string[] settings = style.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

    foreach (string s in settings)
    {
        if (!s.Contains(':'))
            continue;
        string[] data = s.Split(':');
        result.Add(data[0], data[1]);
    }

    return result;
}

此入口是IsNodeVisible,将检查传递给它的HtmlNode的可见性.

The entry point for this is IsNodeVisible and will check the visibility of the HtmlNode passed to it.

这篇关于HtmlAgilityPack:如何检查元素是否可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆