How to activate javascript links using zombie.js

I am trying to get zombie.js to activate a link using javascript. The page I'm testing for 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> 

Script I use:

 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 an 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?

+4
source share
3 answers

Well, it doesn't seem to understand javascript:code hrefs. Perhaps you can get the url, delete the javascript: -section section and evaluate it?

Disclaimer: I have not used zombie.js yet.

0
source

Zombie.js does not process links from "javascript:" to href (at the time of this writing).

I fixed this by adding 3 lines to the source code. Look for / node_modules / zombie / lib / zombie / history.coffee and add 3 lines commented out with FIX (be careful that in. Coffee you have to respect indentation, i.e. Use 2 spaces):

 # 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 use fork zombie.js on github.com and put this in a Pull request, but until then you can use this snippet or make this carry request in front of me.

+6
source

The zombie.js API says that selecting a browser.clickLink() parameter requires a CSS selector or link text. Therefore your code should work.

But try adding the id to the link if you have control over the page and using the CSS selector

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

Source: https://habr.com/ru/post/1392347/


All Articles