简体   繁体   中英

How to retrieve javascript variable (an array) and its content using HtmlUnit

I am using HtmlUnit to get a page from server. the page has lots of javascript and i want to get some variable values (array, json etc) from page. Is there a way to get all the global variables? the code i am using to get variable is as below

ScriptResult scr = result.executeJavaScript("User");
scr.getJavaScriptResult();

when i print this variable it shows [object Object] how do i get properties of this variable. I know that there is a property "id" so i can simply call result.executeJavaScript("User.id") but there are some more which i don't know at this time. how i can see all the properties? same problem is with the Json object

var Item = {Locations: {"1":{"name":"New York", "desc":"xyz"}}

the same issue is here

result.executeJavaScript("Item.Locations");

return [object Object] , Is there any way to get whole json object at once?

好吧,我发现[object object]只是NativeObject的实例,因此只需将其NativeObject并使用迭代Iterator<Entry<Object,Object>> entryset = ((NativeObject)scr.getJavaScriptResult()).entrySet().iterator();遍历Iterator<Entry<Object,Object>> entryset = ((NativeObject)scr.getJavaScriptResult()).entrySet().iterator();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM