简体   繁体   中英

What is proper way to get javascript object by Selenium webdriver in c#

I have javascript object like the following

var jsObj =
    {
        x: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
        y: [[29.9, 71.5, 106.4, 129.2, 144, 176, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4], [24, 70, 19, 128, 148, 178, 131, 140, 211, 190, 91, 50]],
        yname: ["mon-data", "mon-data2"]
    }

I get this object by selenium webdriver in a console application

IJavaScriptExecutor js = webBrowser1 as IJavaScriptExecutor;
string scp = "return window.jsObj ;";
var obj = (object)js.ExecuteScript(scp);

I got the object properly and then i have tried to put the value of the object into array like the following

 string[] xaxis=obj.x;
 string[] name=obj.yname;
 double[][] yaxis = obj.y;

I have tried to do it by looping through the obj but got error. what would be the proper way to handle this kind of object in c#?

Call your object as IEnumerable. It will allow you to use loop\\foreach etc.

foreach (var o in obj as IEnumerable)
{
    Console.WriteLine(o);
}

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