Skip to content

Instantly share code, notes, and snippets.

@linjunpop
Created August 24, 2012 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linjunpop/3447051 to your computer and use it in GitHub Desktop.
Save linjunpop/3447051 to your computer and use it in GitHub Desktop.
Casperjs HTMLElementObject.click()

HTMLElementObject.click(), which is described here:

http://www.w3schools.com/jsref/met_html_click.asp

https://developer.mozilla.org/en-US/docs/DOM/element.click

works on Safari/Chrome.

But in Casperjs:

➜  casperjs --version
1.0.0-RC1

➜  casperjs test.coffee   
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: /Users/linjunpop/Desktop/abc/test.html, HTTP GET
[debug] [phantom] Navigation requested: url=file:///Users/linjunpop/Desktop/abc/test.html, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "file:///Users/linjunpop/Desktop/abc/test.html"
[info] [remote] START click
[info] [remote] [object HTMLHeadingElement]
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 2/2 file:///Users/linjunpop/Desktop/abc/test.html (HTTP 0)
[debug] [phantom] start page is loaded
[info] [phantom] Step 2/2: done in 211ms.
[info] [phantom] Done 2 steps in 308ms
fs = require 'fs'
casper =
require('casper').create
verbose: true
logLevel: "debug"
casper.start "#{fs.workingDirectory}/test.html"
casper.run()
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>A</title>
</head>
<body>
<h1 id='foo' onclick="alert('clicked'); return false;">hi</h1>
<script type="text/javascript" charset="utf-8">
console.log('START click');
console.log(document.getElementById('foo').toString());
console.log(document.getElementById('foo').click());
console.log('END click');
</script>
</body>
</html>
@linjunpop
Copy link
Author

Also, with zombie.js http://zombie.labnotes.org/

Browser = require("zombie")
browser = new Browser()
browser.visit "http://localhost:3000/test.html"

Run:

✗ coffee test_zombie.coffee    
START click
[ H1#foo ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment