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

发布于 2024-10-07 20:56:38 字数 213 浏览 0 评论 0原文

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

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

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

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 element through JavaScript (this works fine) atm, but i have the element in c#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-10-14 20:56:38

不幸的是元素不能像这样传递给 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()"); 更好的方向;诡计。

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.

一梦等七年七年为一梦 2024-10-14 20:56:38

您可以通过以下方式执行此操作:

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

You can do this with the following:

WebBrowser.Navigate("javascript:void(document.getElementById('"+button.Id+"').click())");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文