|
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
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,080
|
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.aspxHope this helps. Please feel free to let us know if you still have any questions. Thanks!
|
|
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
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,080
|
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.
|
|
Rank: Newbie Groups: Member
Joined: 4/16/2018 Posts: 3
|
Thanks for your prompt reply.
Regards, Sowmya
|
|