简体   繁体   English

如何使用HTMLAgility选择类rel

[英]How to select the class rel with HTMLAgility

<div id="id_language" class="sBody text select_m" rel="1">Français</div>

From this HTML code. 从这个HTML代码。 I want HTMLAgility or something else to find it. 我想要HTMLAgility或其他东西来找到它。

And next is my code 接下来是我的代码

 while (htmldoc.DocumentNode.SelectNodes("Français") == null)
                {
                    webBrowser1.Document.GetElementById("id_language").Focus();

                    SendKeys.Send("{DOWN}");
                }

In my code, I want to find to keydown until it found "Français" (or rel=1) and then stop. 在我的代码中,我想找到keydown,直到找到“Français”(或rel = 1)然后停止。 Anyone can help me? 有人可以帮帮我吗? Thank you. 谢谢。

So First Francais is the value of your div node. 所以First Francais是你的div节点的价值。 If you want to search for the div Html Node with value Francais. 如果要搜索值为Francais的div Html节点。 You should have. 你应该有。

var divNode = htmldoc.DocumentNode.Descendants().
                       Where(x => x.Name == "div" && x.InnerText == "Français").
                       FirstOrDefault();

If you want to find the div with attribute rel=1, you should have something like this: 如果你想找到属性为rel = 1的div,你应该有这样的东西:

var divNode = htmldoc.DocumentNode.Descendants().
                   Where(x => x.Name == "div" && x.Attributes["rel"].Value == "1").
                   FirstOrDefault();

This might do the trick for you 这可能对你有所帮助

var french = doc.DocumentNode.SelectSingleNode("//div[@rel]")
var relVal = french.Attributes["rel"].Value;

so you can do 所以你可以做到

while(htmldoc.DocumentNode.SelectSingleNode("//div[@rel]").Attributes["rel"].Value == "1")
{
    SendKeys.Send("{DOWN}");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM