代码之家  ›  专栏  ›  技术社区  ›  martin.malek

如何在GeckoFX中获取Dom树

  •  2
  • martin.malek  · 技术社区  · 17 年前

    我开始玩壁虎。不幸的是,我没有找到任何关于这个的大文档。我需要获取页面的DOM树。迭代每个元素并获取其信息。有人能帮我吗?

    4 回复  |  直到 17 年前
        1
  •  6
  •   Scott    15 年前

    我知道这是一个老问题,但有人可能仍在寻找答案。

    GeckoNodeCollection nodes = geckoWebBrowser1.Document.GetElementsByName("*");
    foreach(GeckoNode node in nodes)
    {
        //do whatever you need to do with the node .. 
        GeckoElement element = node as GeckoElement;
        //..
    }
    
        2
  •  3
  •   Sire    16 年前

    示例代码:

    GeckoElementCollection links = geckoWebBrowser1.Document.GetElementsByTagName("a");
    foreach (GeckoElement link in links) {
        Debug.WriteLine(link.GetAttribute("href"));
    }
    
        3
  •  0
  •   Priyank Bolia    16 年前

    nsIDOMNode 将为您提供DOM遍历能力。您可以从文档节点开始。你可以调查一下 nsInterfaces.cs

        4
  •  0
  •   SuperBisco    9 年前

    如果要使用XPath,可以尝试以下方法:

     browser.LoadXml("<MyTag><div>helloworld</div></MyTag>");
    
     var r = browser.Document.EvaluateXPath("//div");
     Assert.AreEqual(1, r.GetNodes().Count());
    

    因此,在prev代码中:

    GeckoElementCollection nodes = browser.Document.EvaluateXPath("//div").GetNodes();    
    foreach(GeckoNode node in nodes)
    {
        //do whatever you need to do with the node .. 
        GeckoElement element = node as GeckoElement;
        //..
    }
    
    推荐文章