Logo
My Account |  Site Map | Contact Us  
Welcome Guest Search | Active Topics | Sign In | Register

How to return a collection of all child elements with the specified tag name Options
Sowmya
Posted: Monday, April 16, 2018 5:53:41 PM
Rank: Newbie
Groups: Member

Joined: 4/16/2018
Posts: 3
Hi,

How can i achieve this with EO Browser

HtmlElementCollection td_elements = webbrowser.Document.GetElementsByTagName("td"); // Get all <td> definitions
foreach (HtmlElement item in td_elements)
{
HtmlElementCollection a_elements = item.GetElementsByTagName("a"); // Get all <a> definitions in the <td> collection
foreach (HtmlElement a_item in a_elements)
{
if (a_item.OuterText.ToLower() == defaultitem.ToLower()) {// If <a>=default type
var url =a_item["href"].ToString();
// some logic
}
}
}

EO.WebBrowser.DOM.Element doesnt have a GetElementsByTagName() to fetch all <a> definitions form Element[] array.

I tried using EvalScript as below.

var script = @"
var td_elements = document.getElementsByTagName('td');
var a_elements =[];
var count =10;
for (var i=0; i< td_elements.length; i++) {
count++;
var child_elements = td_elements[i].getElementsByTagName('a');
a_elements.push(child_elements);
}
a_elements;
";

JSObject links = (JSObject) webView.EvalScript(script);But always get the Cast exception

An exception of type 'System.InvalidCastException' occurred in th.exe but was not handled in user code

Additional information: Unable to cast object of type 'System.Object[]' to type 'EO.WebBrowser.JSObject'.

Your help will be highly appreciated.

Regards,
Sowmya


eo_support
Posted: Tuesday, April 17, 2018 10:27:40 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 20,678
Hi,

You can try to return value this way using an anonymous function:

Code: C#
webView.EvalScript(@"(function() 
{ 
    ---- your code here.....
    return ret_value; 
})();");


You should not return an array of elements though since each element is still a reference and it's very expensive performance wise when later that element is accessed (such as getting a property value). See here for more explanation (look for "performance" section):

https://www.essentialobjects.com/doc/webbrowser/advanced/jsdom.aspx

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

Thanks!
Sowmya
Posted: Tuesday, April 17, 2018 4:55:37 PM
Rank: Newbie
Groups: Member

Joined: 4/16/2018
Posts: 3
Hi,

Thanks for getting back.

I will have to fetch all the child <a> elements under <td> elements. Even by using anonymous function, how can i get back these without using array of elements? what should be the type of ret_value in this case?

Kindly explain.

Regards,
Sowmya

eo_support
Posted: Wednesday, April 18, 2018 7:46:08 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 20,678
What do you want from that array of elements? The answer to that question would be what you should return. Basically you should avoid passing a reference from JavaScript to .NET. For example, if you wish to get an element's innerHTML, then you have two choices:

1. On the first call to return the element. Then do a second call to get the element's InnerHTML;
2. Return element.InnerHTML directly in one call;

The second option is more efficient than the first option because it's only one call instead of two. The idea here is not that option 1 is wrong, but it's less efficient than option 2. Likewise, returning an array of elements is not wrong, but you might be able to make it more efficient to return the value you .NET code wanted directly.
Sowmya
Posted: Wednesday, April 18, 2018 4:21:06 PM
Rank: Newbie
Groups: Member

Joined: 4/16/2018
Posts: 3
Thanks for your prompt reply.

Regards,
Sowmya


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.