简体   繁体   中英

DotNetBrowser check if element is visible

I am trying to implement the following JavaScript function to check if an element is visible or not. The issue is that javascript variable is null and cast AsFunction not working of course.

var element = node as DOMElement;
JSValue javascript = browser.ExecuteJavaScriptAndReturnValue("var KBrowserIsHidden = function (el) { " +
                "var style = window.getComputedStyle(el); " +
                "return (style.display === 'none' || style.visibility === 'hidden') " +
                "}" +
                "KBrowserIsHidden");
var hidden = Convert.ToBoolean(javascript.AsFunction().InvokeAndReturnValue(null, element));

Regarding obtaining the javascript function, please consider separating the function definition and returning it to the C# side. The sample source code is demonstrated below:

browser.ExecuteJavaScriptAndReturnValue("var KBrowserIsHidden = function (el) { " +
"var style = window.getComputedStyle(el); " +
"return (style.display === 'none' || style.visibility === 'hidden'); " +
"};");

JSValue javascript = browser.ExecuteJavaScriptAndReturnValue("KBrowserIsHidden;");

Also, please keep in mind that DotNetBrowser does not allow passing the DOM API objects to the JavaScipt - .NET Bridge with the automatic conversion. In the described case, I would like to recommend that you find the required element using the JavaScript code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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