Welcome Guest Search | Active Topics | Sign In | Register

EO.WebBRowser check div style (display property) Options
Cipri
Posted: Thursday, October 2, 2014 12:20:02 PM
Rank: Newbie
Groups: Member

Joined: 10/1/2014
Posts: 6
Hi,

I need to know if a div in the currently loaded screen is visible or not.

The html code is the following:

<div id="_swescrnbar" class="addshowactivated" style="display: none;">

So what I want to do is check the 'display' property value, in this case = none.

Depending on the value I need to do different things in C# code, so, how can I assign the display value to a string?

Please note, the value needs to go to a C# string, I need to change the program depending on the value, not the screen, so doing it in javascript wont work.


Among other things, I have tried this:

Element _swescrnbar = doc.getElementById("_swescrnbar");
if (_swescrnbar == null)
return false;

string style = (string)_swescrnbar["style"];


With no success: Unable to cast object of type 'b' to type 'System.String'.

Best regards,
Cipriano
eo_support
Posted: Thursday, October 2, 2014 5:38:44 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
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:

Code: JavaScript
div.style;


To:

Code: JavaScript
div.style.display;


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.aspx

The 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!
Cipri
Posted: Friday, October 3, 2014 1:43:43 PM
Rank: Newbie
Groups: Member

Joined: 10/1/2014
Posts: 6
Awesome, it worked great! Thank you very much


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.