Hi,
For such situation you will always want to do it with JavaScript first. Once you have the correct JavaScript code, you can call WebView.EvalScript with the correct JavaScript code.
For example, your code is essentially the same as the following EvalScript code:
Code: C#
string style = (string)webView.EvalScript(@"
var div = doc.getElementById('_swescrnbar');
div.style;
");
Note that the last line of the code is just "div.style", not "return div.style" as you would normally write in a function. This is because EvalScript evaluates the script in the page's global context, not inside a local function context. The EvalScript call returns the value of the last statement in the JavaScript code you passed in. In this case it would be the value of "div.style".
The above code
does not work (you will get the same InvalidCastException as you were getting) because your JavaScript code is wrong. div.style returns a style object. Different browsers have different internal representation for a style object. For example, in IE it is an IHTMLElementStyle object and in FireFox it is an CSSStyleDeclaration object. But in all browsers, a style object is not a string object. Unless it's already a string object in JavaScript, you won't get a string on the C# side. If it is already a string in JavaScript, you will get a string in C#. That's why the focus should always be getting the correct JavaScript code.
In your case, you can change the JavaScript code from:
To:
That should give you the correct display value since div.style.display will return a string value in JavaScript. Thus when it comes to the C# side it becomes a string value as well.
As a simple test, you can use typeof keyword in JavaScript to check the type of a JavaScript value. All the simple values in JavaScript will be automatically converted to their corresponding C# type when they travels from JavaScript to C#. For example, a JavaScript integer will become a C# int. All other JavaScript object value will become a C# JSObject:
http://www.essentialobjects.com/doc/6/eo.webbrowser.jsobject.aspxThe type "b" you encountered in your exception is an internal class that derives from JSObject. You can not cast a JSObject directly to a string.
Hope this helps. Please feel free to let us know if you still have any more questions.
Thanks!