GeckoFX Source获取DOM /隐藏的div元素

时间:2018-08-19 00:50:27

标签: c# dom xpath nodes geckofx

我正在尝试替换特定div的内部HTML。它通过Gecko浏览器和视图源显示,但是当元素集合发送到文本框时,找不到div。这是我尝试过的一些方法。非常感谢您的帮助。

//NODES
                GeckoNodeCollection nodes2 = mainbrowser.Document.GetElementsByClassName("*");
                foreach (GeckoNode node in nodes2)
                {
                    GeckoElement element3 = node as GeckoElement;
                }
                GeckoNodeCollection nod = mainbrowser.Document.GetElementsByClassName("minin-class");
                foreach (GeckoNode node in nod)
                {
                    if (NodeType.Element == node.NodeType)
                    {

                        try
                        {
                            GeckoElement ele = (GeckoElement)node;
                        }
                        catch (Exception ex)
                        {
                            string ep = ex.ToString();
                            GeckoHtmlElement ele = (GeckoHtmlElement)node;
                        }
                    }
                }

//ELEMENT COLLECTION
                GeckoElementCollection element = mainbrowser.Document.GetElementsByTagName("div");
                    foreach (GeckoHtmlElement curelement in element) {
                    if (curelement.GetAttribute("id") == "minin") {
                        curelement.InnerHtml = ("hello");
                    }
                }

//DOM
                Gecko.DOM.GeckoLinkElement element6 = new Gecko.DOM.GeckoLinkElement(mainbrowser.Document.GetElementById("minin").DomObject);
                element6.InnerHtml = "Hello";

//XPATH-ATTEMPT
                //var r = mainbrowser.Document.EvaluateXPath("//*[@id='minin']");
                //Assert.AreEqual(1, r.GetNodes().Count());

                //GeckoElementCollection nodes = mainbrowser.Document.EvaluateXPath(x.ToString()).GetNodes();
                //foreach (GeckoNode node in nodes)
                //{
                  //  GeckoElement element4 = node as GeckoElement;
                //}

2 个答案:

答案 0 :(得分:1)

当然可以做到,而且也很容易:

foreach(var n in mainbrowser.Document.Body.GetElementsByTagName("div"))
{
   GeckoHtmlElement hnode = n as GeckoHtmlElement;
   if(hnode.Id == "minin")
   {
      //suppose this DOM: <div id="minin"><span> something </span></div>

      hnode.InnerHtml = "<div><h2>hi!</h2></div>";
      //outerHtml = "<div id="minin"><div><h2>hi!</h2></div></div>"

      hnode.TextContent = "hi";
      // outerHtml = "<div id="minin">hi</div>"

      hnode.SetAttribute("style", "color: red;");
      //outerHtml = "<div style="color: red;" id="minin">hi</div>";
   }
}

也许您喜欢使用javascript

 using (AutoJSContext java = new AutoJSContext(geckoWebBrowser1.Window))
 {
     string value = "hello there";
     string outp = "";
     java.EvaluateScript("document.getElementById('minin').innerText ='" + value +"'", out outp);
 }

注意::geckoHtmlElement类具有Id属性,该属性具有html标签的id属性的值

<tagName id = "myid">

答案 1 :(得分:0)

我认为不可能,所以改用硒Web驱动程序。