使用 GeckoFX 网络浏览器,是否可以像这样通过 JavaScript 传递 GeckoElement,
WebBrowser.Navigate("javascript:void("+ele.DomObject+".onclick())");
我正在通过 JavaScript(这很好)atm 选择 DOM 元素,但我在 c# 中有该元素。
使用 GeckoFX 网络浏览器,是否可以像这样通过 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();
我不确定此代码段是否会直接帮助您,因为它主要专注于使用控件的GFXe构建,但我相信它会为您指明比
WebBrowser.Navigate("javascript:hack.goesHere()"); 诡计。
您可以使用以下方法执行此操作:
WebBrowser.Navigate("javascript:void(document.getElementById('"+button.Id+"').click())");