简体   繁体   English

在C#中使用CEFSharp访问DOM元素

[英]Accessing DOM Elements using CEFSharp in C#

I am working on a small project based on CEFSharp in C# . 我正在一个基于C# CEFSharp的小项目中工作。

I want to know the procedure to access DOM Elements using CEFSharp in C# . 我想知道在C#使用CEFSharp访问DOM元素的过程。 I have all initial things setup already, just need to access the elements. 我已经完成所有初始设置,只需要访问元素即可。 I know how to do this with the built-in web Browser of C# but I cannot find any proper information about doing this with CEFSharp . 我知道如何使用C#的内置Web浏览器执行此操作,但是我找不到有关使用CEFSharp执行此操作的任何适当信息。

I am using the following line of code to access the DOM element of the webpage in a button click listener (separate class than browser class). 我正在使用以下代码行通过按钮单击侦听器(与浏览器类不同的类)访问webpageDOM元素。

private void button1_Click(object sender, EventArgs e)
{brow.chromeBrowser.ExecuteScriptAsync("document.getElementsByName('q').value='Hello';");
}

where brow=CEF browser class object, chromeBrowser=browser instance 其中brow = CEF浏览器类对象,chromeBrowser =浏览器实例

With the above code modified like ("alert('My Message')"), I can get an alert message easily with the JavaScript , no problem with that, but I want to access a specific element of DOM , and here nothing works. 通过将上面的代码修改为(“ alert('My Message')”),我可以使用JavaScript轻松获得警报消息,这没有问题,但是我想访问DOM的特定元素,因此这里没有任何作用。

Well I found the answer by myself, just a slight change. 好吧,我自己找到了答案,只是稍有变化。 Hope it helps someone else too 希望它也能帮助别人

brow.chromeBrowser.ExecuteScriptAsync("document.getElementsByName('q')[0].value='Hello';");

It was a small typo in javascript line . 这是javascript行中的一个小错字。 I wrote [0] in the above syntax and everything worked fine. 我用上述语法写了[0],一切正常。 The textfield on the webpage got filled with my text (Hello) after my button click. 单击按钮后,网页上的文本字段充满了我的文本(Hello)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM