Pass GeckoFX DOM elements to JavaScript navigation calls
using GeckoFX web browser, is it possible to pass a GeckoElement through JavaScript like this,
WebBrowser.Navigate("javascript:void("+ele.DomObject+".onclick())");
I'm selecting the DOM ele开发者_JAVA百科ment through JavaScript (this works fine) atm, but i have the element in c#.
Unfortunately elements can't be passed to javascript like that.
However, the WebBrowser.Navigate call is unnecessary and causes an unneeded loss of page variables.
For the sake of completeness I've posted a snippet - long winded for this occasion ;) - that injects javascript and then calls it from an automated button click via a button.click() handler without the need to navigate the browser to run it all.
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();
I'm not sure this snippet will help you, directly, as it's mainly focused on using the GFXe build of the control but, I'm sure it will point you in a better direction than the
WebBrowser.Navigate("javascript:hack.goesHere()"); trick.
You can do this with the following:
WebBrowser.Navigate("javascript:void(document.getElementById('"+button.Id+"').click())");
精彩评论