如何激活与zombie.js的JavaScript链接

我试图让zombie.js来激活一个使用JavaScript的链接。 我正在testing的页面是:

<html> <body> <div id="test123"> START_TEXT </div> <a href="javascript:go()">GO</a><br/> <script type="text/javascript"> go = function() { var el = document.getElementById("test123"); el.innerHTML = "CHANGED"; } </script> </body> </html> 

我正在使用的脚本是:

 var zombie = require("zombie"); var browser = new zombie.Browser; browser.visit( "http://localhost:8000/testpage.html", function() { browser.clickLink("GO", function(e, browser, status) { var temp = browser.text("div#test123"); console.log("content:", temp); }); }); 

我收到错误消息:

 node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Cannot load resource: javascript:go() at History._resource (/home/julian/temp/node_modules/zombie/lib/zombie/history.coffee:75:15) at History._pageChanged (/home/julian/temp/node_modules/zombie/lib/zombie/history.coffee:60:21) at History._assign (/home/julian/temp/node_modules/zombie/lib/zombie/history.coffee:213:19) at Object.location (/home/julian/temp/node_modules/zombie/lib/zombie/history.coffee:51:24) at Object.click (/home/julian/temp/node_modules/zombie/lib/zombie/jsdom_patches.coffee:31:59) at Object.dispatchEvent (/home/julian/temp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:480:47) at /home/julian/temp/node_modules/zombie/lib/zombie/eventloop.coffee:130:16 at EventLoop.perform (/home/julian/temp/node_modules/zombie/lib/zombie/eventloop.coffee:121:7) at EventLoop.dispatch (/home/julian/temp/node_modules/zombie/lib/zombie/eventloop.coffee:129:19) at Browser.dispatchEvent (/home/julian/temp/node_modules/zombie/lib/zombie/browser.coffee:220:30) 

当我使用

browser.evaluate("go()")

有用。 我错过了什么?

那么它似乎并不理解javascript:code hrefs。 也许你可以得到的url,删除javascript:-section并评估它?

免责声明:我还没有使用zombie.js。

Zombie.js不处理href(在写这篇文章的时候)的“javascript:”的链接。

我通过在源代码中添加3行来修复它。 寻找/node_modules/zombie/lib/zombie/history.coffee并添加3条FIX注释(注意,在.coffee中你必须尊重缩进,即使用2个空格):

 # Location uses this to move to a new URL. _assign: (url)-> url = @_resolve(url) # FIX: support for javascript: protocol href if url.indexOf("javascript:")==0 @_browser.evaluate(url.substr("javascript:".length)) return was = @_stack[@_index]?.url # before we destroy stack @_stack = @_stack[0..@_index] @_stack[++@_index] = new Entry(this, url) @_pageChanged was 

我可能应该在github.com上使用zombie.js,并把它放到一个Pull Request中,但是在这之前,欢迎您使用这个代码片段,或者在我之前提出这个请求。

zombie.js API表示它将采用CSSselect器或链接文本作为browser.clickLink()的第一个参数。 所以你的代码应该工作。

但是尝试添加一个id到链接,如果你有控制页面,并使用CSSselect器

 browser.clickLink('#thelink', function(e, browser, status) { var temp = browser.text("div#test123"); console.log("content:", temp); });