GeckoFX Source 获取 DOM / 隐藏的 div 元素
Posted
技术标签:
【中文标题】GeckoFX Source 获取 DOM / 隐藏的 div 元素【英文标题】:GeckoFX Source Get DOM / hidden div elements 【发布时间】:2019-01-25 13:43:18 【问题描述】:我正在尝试替换特定 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;
//
【问题讨论】:
【参考方案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">
【讨论】:
【参考方案2】:我认为不可能,所以改用 selenium 网络驱动程序。
【讨论】:
这不是一个真正的答案。以上是关于GeckoFX Source 获取 DOM / 隐藏的 div 元素的主要内容,如果未能解决你的问题,请参考以下文章
GeckoFX ObjectForScripting,GeckoFX 下js调用c#方法