【问题标题】:How do I access the title attribute with xpath?如何使用 xpath 访问 title 属性?
【发布时间】:2018-05-22 11:43:45
【问题描述】:

所以我试图访问颜色部分中的title 属性。 因此,如果您将鼠标悬停在产品右侧的任何小图像上,您会看到上面写着Color 名称。 我已经设法导航到那里,但我一生都无法弄清楚如何获得title 属性。目前它正在打印节点,但我想访问 title 属性。

如何正确访问title属性并打印出图片对应的颜色?

This is the test link I am using (AliExpress)

Console.WriteLine("Product URL: ");
            //Declare the URL
            string url = Console.ReadLine();
            // HtmlWeb - A Utility class to get HTML document from http
            var web = new HtmlWeb();
            //Load() Method download the specified HTML document from an Internet resource.
            var doc = web.Load(url);

            var nodes = doc.DocumentNode.SelectNodes("//li[@class = 'item-sku-image']");
            foreach (var node in nodes)
            {
                //var colors = node.Attributes["/a[title]"].Value;
                Console.WriteLine(node);

            }
            Console.ReadLine();

【问题讨论】:

    标签: c# .net xpath nodes html-agility-pack


    【解决方案1】:

    您可以尝试迭代以下 X-Path://li[@class = 'item-sku-image']/a/img/@title,或者将其替换为://li[@class = 'item-sku-image'],并替换为://li[@class = 'item-sku-image']/a/img,然后检查节点的属性。

    它应该产生一系列包含你所追求的标题的字符串。

    【讨论】:

    • 我会试试的!另外,在学习 xpath 时,在遍历节点时是否应该遵循一组规则?
    • 老实说,我并没有过多地使用 X-Path。如果可能的话,我建议不要使用 CSS 类,而是坚持使用 Id,并尽量保持简单。
    • @npinto 如何从节点中提取标题内容? InnerText 返回一个空字符串
    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2017-08-17
    相关资源
    最近更新 更多