[英]How to activate javascript links with zombie.js
I am trying to get get zombie.js to activate a link that uses javascript. 我想让zombie.js激活一个使用javascript的链接。 The page I am testing it on is:
我正在测试它的页面是:
<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>
The Script I am using is: 我使用的脚本是:
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);
});
});
I get the error message: 我收到错误消息:
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)
When I use 我用的时候
browser.evaluate("go()")
it works. 有用。 What am I missing?
我错过了什么?
Zombie.js doesn't handle links with "javascript:" on href (at the time of this writing). Zombie.js不会在href上处理带有“javascript:”的链接(在撰写本文时)。
I fixed it by adding 3 lines to the source code. 我通过在源代码中添加3行来修复它。 Look for /node_modules/zombie/lib/zombie/history.coffee and add the 3 lines commented with FIX (beware that in .coffee you must respect indentation, ie. use 2 spaces):
查找/node_modules/zombie/lib/zombie/history.coffee并添加用FIX注释的3行(请注意,在.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
I probably should fork zombie.js on github.com and put this into a Pull Request, but until then you are welcome to do use this snippet, or make that pull request before me. 我可能应该在github.com上分叉zombie.js并将其放入Pull请求中,但在此之前,欢迎您使用此代码段,或者在我之前提出拉取请求。
Well it doesn't seem to understand javascript:code
hrefs. 好吧它似乎不理解
javascript:code
hrefs。 Perhaps you can get the url, remove the javascript:-section and evaluate it? 也许你可以获取网址,删除javascript:-section并评估它?
Disclaimer: I haven't used zombie.js myself yet. 免责声明:我自己还没有使用过zombie.js。
The zombie.js API says it takes a CSS selector or the link text as the first parameter for browser.clickLink()
. zombie.js API表示它将CSS选择器或链接文本作为
browser.clickLink()
的第一个参数。 So your code should work. 所以你的代码应该工作。
But try adding an id to the link, if you have control over the page, and using a CSS selector 但是,如果您可以控制页面并使用CSS选择器,请尝试在链接中添加id
browser.clickLink('#thelink', function(e, browser, status) {
var temp = browser.text("div#test123");
console.log("content:", temp);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.