GeckoFX 来源 获取 DOM / 隐藏 div 元素
GeckoFX Source Get DOM / hidden div elements
我正在尝试替换特定 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;
//}
我认为这不可能,所以改用 selenium 网络驱动程序。
当然可以,而且也很容易:
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 class 的 Id 属性 具有 html 标签的 id 属性值
<tagName id = "myid">
我正在尝试替换特定 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;
//}
我认为这不可能,所以改用 selenium 网络驱动程序。
当然可以,而且也很容易:
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 class 的 Id 属性 具有 html 标签的 id 属性值
<tagName id = "myid">