Friday, 15 April 2011

javascript - Printing elements from XPath results in more than the elements -



javascript - Printing elements from XPath results in more than the elements -

all want homecoming string xpath determined i'm having problem using getelementsbyxpath function in casperjs.

class="lang-js prettyprint-override">var casper = require('casper').create({ verbose: false, loglevel: 'debug' }); casper.useragent("mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/28.0.1500.72 safari/537.36"); casper.start(); casper.thenopen('http://www.uky.edu', function(){ content = casper.evaluate(function () { homecoming __utils__.getelementsbyxpath('//div[@id=\'container\']/section[@id=\'content\']/aside[@class=\'socialtab clearfix\']/div[@id=\'usual2\']/div[@id=\'tabs1\']/figure[@class=\'youtube\']/h4/a'); }); }); casper.run(function() { this.echo(json.stringify(content)); this.echo('completed').exit(); });

doesn't stop returning data. , not returning particular string i'm looking for, seems it's returning info entire webpage instead.

i ended trying practically blank webpage 1 div , got same issue used //div xpath , received following

[{"align":"","attributes":{"length":0},"baseuri":"http://download2012.ad.uky.edu/caspertest.php","childelementcount":0,"childnodes":{"0":null,"length":1},"child ren":{"length":0},"classlist":{"length":0},"classname":"","clientheight":20,"cli entleft":0,"clienttop":0,"clientwidth":384,"contenteditable":"inherit","dataset" :{},"dir":"","draggable":false,"firstchild":null,"firstelementchild":"","hidden" :false,"id":"","innerhtml":"test2","innertext":"test2","iscontenteditable":false ,"lang":"","lastchild":{"attributes":"","baseuri":"http://download2012.ad.uky.ed u/caspertest.php","childnodes":{"length":0},"data":"test2","firstchild":"","last child":"","length":5,"localname":"","namespaceuri":"","nextsibling":"","nodename ":"#text","nodetype":3,"nodevalue":"test2","ownerdocument":null,"parentelement": null,"parentnode":null,"prefix":"","previoussibling":"","textcontent":"test2","w holetext":"test2"},"lastelementchild":"","localname":"div","namespaceuri":"http: //www.w3.org/1999/xhtml","nextelementsibling":"","nextsibling":null,"nodename":" div","nodetype":1,"nodevalue":"","offsetheight":20,"offsetleft":8,"offsetparent" :{"alink":"","attributes":{"length":0},"background":"","baseuri":"http://downloa d2012.ad.uky.edu/caspertest.php","bgcolor":"","childelementcount":1,"childnodes" :{"0":null,"1":null,"2":null,"length":3},"children":{"0":null,"length":1},"class list":{"length":0},"classname":"","clientheight":300,"clientleft":0,"clienttop": 0,"clientwidth":400,"contenteditable":"inherit","dataset":{},"dir":"","draggable ":false,"firstchild":{"attributes":"","baseuri":"http://download2012.ad.uky.edu/ caspertest.php","childnodes":{"length":0},"data":"test\n","firstchild":"","lastc hild":"","length":5,"localname":"","namespaceuri":"","nextsibling":null,"nodenam e":

and on.

you cannot homecoming dom elements page context in casperjs. if document.queryselectorall used instead of __utils__.getelementsbyxpath (with adjusted css selector), result array of undefined values. not case __utils__.getelementsbyxpath. snapshots of dom elements returned partially serializable. problem contain circular references document , grow continuously.

from docs:

note: arguments , homecoming value evaluate function must simple primitive object. rule of thumb: if can serialized via json, fine.

closures, functions, dom nodes, etc. not work!

you need want in page context. illustration element string:

content = casper.evaluate(function () { homecoming __utils__.getelementbyxpath(someselector).outerhtml; });

javascript xpath phantomjs casperjs selectors-api

No comments:

Post a Comment