przemoinho
przemoinho

Reputation: 201

PhantomJS - click element

i want to click element on the website and then take a screenshot, how to do it in PahantomJS?

Here is my html:

<html>
<head>
    <title>list</title>
</head>
<body class="directory">
    <input id="search" type="text" placeholder="Search" autocomplete="off">
    <div id="wrapper">
      <h1> / </h1>
      <ul id="files" class="view-tiles">
        <li><a href="/build" class="" title="build"><span class="name">build</span><span class="size">0</span><span class="date">Wed Oct 12 2016 </span></a></li>
      </ul>
    </div>
</body>
</html>

and that what i tried

var page = require('webpage').create();

page.onError = function(msg, trace) {
  return;
};

page.open("http://localhost:9001/", function(status){
    if (status !== 'success') {
        console.log('Unable to load the url!');
        // phantom.exit();
    } else {

        console.log("click");
        page.render('1.png');

            document.getElementById("build").click();

        page.render('2.png');


    page.onConsoleMessage = function(msg, lineNum, sourceId) {
        console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');

        if (msg === "finished") {

            console.log("click");


            }
        };
    // phantom.exit();
    }


    // phantom.exit();
});

How to click it and then take a screenshot to be sure that I move to next page.

Upvotes: 0

Views: 1882

Answers (1)

japrescott
japrescott

Reputation: 5015

if you want to access the dom/window of the page loaded by phantomjs, you will need to wrap your click code within a page.evaluate function. It will be executed sync

So your code would look like this

console.log("click");
page.render('1.png');
page.evaluate(function(selector){
    return document.getElementById(selector).click();
}, 'build');
page.render('2.png');

Upvotes: 1

Related Questions