简体   繁体   中英

How to parse returned css information using Selenium webdriver.executescript

Newbie to Javascript in Node environment...

Using this code below where the script is getting css values for a particular web element, how do I parse this in javascript?

    driver.executeScript(script, ele).then(p => {
       console.log(p);            
    })

The code above shows this result in the console.

{ class: 'container ng-scope', 'ng-controller': 'CalcCtrl' }

It's returned as type Object, so I can't figure out for example how to get the value of the key "class"...

If I change the code to this:

driver.executeScript(script, ele).then(p => {
       console.log(p);            
       var obj = JSON.parse(p);
       console.log(obj.class);
    })

I get this error... 在此处输入图片说明

Where column 27 is the parse in JSON.parse...

The answer to this question, is this:

    driver.executeScript(script, ele).then(p => {
      if(p.class) console.log(p.class);               
    })

JavaScript returns undefined for any key in a JSON object that cannot be found, therefore, the If(p.class) ensures this item of data has a key named class and prints it out.

在此处输入图片说明

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