|
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
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,218
|
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!
|
|
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
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,218
|
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!
|
|
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.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,218
|
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.aspxAnd this property (C# indexer): http://www.essentialobjects.com/doc/6/eo.webbrowser.jsobject.item1.aspxhttp://www.essentialobjects.com/doc/6/eo.webbrowser.jsobject.item2.aspxFor 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.aspxStep 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!
|
|
Rank: Advanced Member Groups: Member
Joined: 9/3/2014 Posts: 68
|
Thank you. There are lots of hidden things behind JSObject class.
|
|
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....
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,218
|
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!
|
|
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.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,218
|
Thanks for sharing!
|
|