Welcome Guest Search | Active Topics | Sign In | Register

EO.WebBRowser get element parent or child node Options
Cipri
Posted: Wednesday, October 1, 2014 11:15:39 AM
Rank: Newbie
Groups: Member

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

I would like to know how to get an element parent or child node on my C# app.

I am trying to change some properties of an element but this particular element does not have a name or an id, the parent div has and id, so I would like to do something like this:

Element div = document.getElementById("myDiv");
div.child[0]["class"]="some class here";


How can this be done?

Best regards,
Cipriano
eo_support
Posted: Wednesday, October 1, 2014 11:28:42 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,080
Hi,

You can just find out the correct JavaScript code to do that and then call EvalScript. For example, if the script code is something like this:

Code: JavaScript
var div = document.getElementById('myDiv');
div.firstChild.className = 'some_class_here';


Then you can write your C# code like this:

Code: C#
webView1.EvalScript(@"
    var div = document.getElementById('myDiv');
    div.firstChild.className = 'some class here';
");


Note that sometimes some JavaScript that works in one browser may not work in another. EO.WebBrowser is based on Google Chrome. So you will want to test your JavaScript with Google Chrome browser if you run into any problem.

Thanks!
Cipri
Posted: Wednesday, October 1, 2014 1:27:05 PM
Rank: Newbie
Groups: Member

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

Thank you very much for your reply.

I was aware I could do it using javasscript but I was trying to avoid using it, is there no other way?

Best regards,
Cipriano
eo_support
Posted: Wednesday, October 1, 2014 2:04:18 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,080
Hi,

Using EvalScript is the most direct and efficient way. Other ways internally go through JavaScript engine and may need custom JavaScript code as well.

Thanks!
Khoa
Posted: Friday, October 3, 2014 10:55:09 PM
Rank: Advanced Member
Groups: Member

Joined: 9/3/2014
Posts: 68
Hi,

Without using EvalScript to evaluate JavaScript code in C#, you can internally go through JavaScript engine to define custom JavaScript function. Could you please explain more how to do that to extend Element class to have custom C# method like element.child[0]["class"]="some class here";

Thanks.
eo_support
Posted: Saturday, October 4, 2014 10:59:23 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,080
Hi,

The Element class derives from JSObject. Pretty much everything is implemented through this method:

http://www.essentialobjects.com/doc/6/eo.webbrowser.jsobject.invokefunction.aspx

And this property (C# indexer):

http://www.essentialobjects.com/doc/6/eo.webbrowser.jsobject.item1.aspx
http://www.essentialobjects.com/doc/6/eo.webbrowser.jsobject.item2.aspx

For example, the source code for Element.id property is like this:

Code: C#
public string id
{
    get { return this["id"] as string; }
    set { this["id"] = value; }
}


To have your own custom element types you will need to:

1. Define your own class that derives from JSObject or Element and implement additional property/method on your class;
2. Call EvalScript with the corresponding JavaScript code/expression to get a JSObject;
3. Call the following method to cast a JSObject into your specific type:

http://www.essentialobjects.com/doc/6/eo.webbrowser.jsobject.castto__1.aspx

Step 3 is necessary because the first object you get is always a JSObject. So you must cast it to your own type. As an example, this is the source code for WebView.GetDOMWindow:

Code: C#
public DOM.Window GetDOMWindow()
{
    return JSObject.CastTo<DOM.Window>(EvalScript("window", false));
}


Hope this makes sense to you. Please feel free to let us know if you still have any questions.

Thanks!
Khoa
Posted: Monday, October 6, 2014 1:52:53 PM
Rank: Advanced Member
Groups: Member

Joined: 9/3/2014
Posts: 68
Thank you. There are lots of hidden things behind JSObject class.
joinyasin
Posted: Monday, June 27, 2016 11:10:53 AM
Rank: Member
Groups: Member

Joined: 3/17/2016
Posts: 12
Please help
may i know how i can get current element property
for example if i open google.com in eo.webbrowser webcontrol
and when i click in search box its mean now current element is text box
so how i can get the tagName "input" and the name property "q" for this tag
please help me for this to know any active element property
Thanks and waiting for reply....
eo_support
Posted: Monday, June 27, 2016 12:19:02 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,080
Hi,

You can use something like this:

Code: C#
//Get the current element
JSObject activeElement = (JSObject)webView.EvalScript("document.activeElement");

//Get the name attribute
string name = (string)activeElement["name"];


Thanks!
Dennis Wallen
Posted: Monday, March 2, 2020 10:37:22 PM
Rank: Newbie
Groups: Member

Joined: 3/2/2020
Posts: 1
I realize this post is old, but thought this might help someone else. I too wanted to keep the keep DOM traversal in my .NET code. This worked for me...

Dim Parent As EO.WebBrowser.DOM.Element = EO.WebBrowser.JSObject.CastTo(Of EO.WebBrowser.DOM.Element)(e.Item("parentNode"))

Where "e" is the starting element. So you can access the parent using e.Item("parentNode"). The rest of the above just casts the result to the .NET Element type.
eo_support
Posted: Wednesday, March 4, 2020 10:10:03 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,080
Thanks for sharing!


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.