C# 将GeckoFX DOM元素传递给JavaScript导航调用

C# 将GeckoFX DOM元素传递给JavaScript导航调用,c#,javascript,dom,geckofx,C#,Javascript,Dom,Geckofx,使用GeckoFX web浏览器,是否可以像这样通过JavaScript传递GeckoElement WebBrowser.Navigate("javascript:void("+ele.DomObject+".onclick())"); 我通过JavaScript(这很好)atm选择DOM元素,但我在c#中有这个元素。不幸的是,元素不能像那样传递给JavaScript 但是,WebBrowser.Navigate调用是不必要的,它会导致页面变量的不必要的丢失 为了完整起见,我已经发布了一个片

使用GeckoFX web浏览器,是否可以像这样通过JavaScript传递GeckoElement

WebBrowser.Navigate("javascript:void("+ele.DomObject+".onclick())");

我通过JavaScript(这很好)atm选择DOM元素,但我在c#中有这个元素。

不幸的是,元素不能像那样传递给JavaScript

但是,WebBrowser.Navigate调用是不必要的,它会导致页面变量的不必要的丢失

为了完整起见,我已经发布了一个片段——这是一个冗长的场合;)-这将注入javascript,然后通过button.click()处理程序从自动按钮单击调用它,而无需导航浏览器来运行它

DOM.GeckoScriptElement script = Document.CreateElement("script").AsScriptElement();
script.Type = "text/javascript";
script.Text = "function doAlert(){ alert('My alert - fired by automating a button click on the [Automated Button]'); }";
Document.Body.AppendChild(script);

script = Document.CreateElement("script").AsScriptElement();
script.Type = "text/javascript";
script.Text = "function callDoAlert(id){ var el = document.getElementById(id); el.click(); }";
Document.Body.AppendChild(script);

DOM.GeckoInputElement button = Document.CreateElement("input").AsInputElement();
button.Type = "button";
button.Id = "myButton";
button.Value = "Automated Button";
button.SetAttribute("onclick", "javascript:doAlert();");

Document.Body.AppendChild(button);

DOM.GeckoInputElement button2 = Document.CreateElement("input").AsInputElement();
button2.Type = "button";
button2.Id = "myOtherButton";
button2.Value = "Press Me";
button2.SetAttribute("onclick", "javascript:document.getElementById('myButton').click();");

Document.Body.AppendChild(button2);

//uncomment to fully automate without the <webbrowser>.Navigate("javascript:.."); hack
//button2.click();
DOM.GeckoScriptElement script=Document.CreateElement(“脚本”).AsScriptElement();
script.Type=“text/javascript”;
script.Text=“function doAlert(){alert('My alert-通过自动点击[自动按钮]”;}按钮触发);
Document.Body.AppendChild(脚本);
script=Document.CreateElement(“脚本”).AsScriptElement();
script.Type=“text/javascript”;
script.Text=“function callDoAlert(id){var el=document.getElementById(id);el.click();}”;
Document.Body.AppendChild(脚本);
DOM.GeckoInputElement button=Document.CreateElement(“输入”).AsInputElement();
button.Type=“button”;
button.Id=“myButton”;
button.Value=“自动按钮”;
SetAttribute(“onclick”,“javascript:doAlert();”;
Document.Body.AppendChild(按钮);
DOM.GeckoInputElement button2=Document.CreateElement(“输入”).AsInputElement();
button2.Type=“button”;
button2.Id=“myOtherButton”;
按钮2.Value=“按我”;
button2.SetAttribute(“onclick”,“javascript:document.getElementById('myButton')。单击();”;
Document.Body.AppendChild(按钮2);
//取消注释以完全自动化,而不使用.Navigate(“javascript:…”);乱劈
//按钮2.单击();
我不确定这段代码能否直接帮助您,因为它主要关注控件的构建,但我相信它将为您指明一个比


WebBrowser.Navigate(“javascript:hack.goesher()”);诀窍。

您可以通过以下方法完成此操作:

WebBrowser.Navigate("javascript:void(document.getElementById('"+button.Id+"').click())");

邪恶的。我目前正在考虑将选中的GeckoFX DOM转换为JS友好的可行走DOM,这种方法比我一直使用的导航黑客要好得多。我必须测试一下这个方法,谢谢scott。