title: CasperJS can't find element using CSS selector or xPath
body: <p>I'm trying to click on the 'Next' button on the Google search using CasperJS and I'm getting the following error:</p>
  
  <p><code>CasperError: Cannot dispatch mousedown event on nonexistent selector: #pnnext</code></p>
  
  <p>The button is conveniently tagged with the id <code>#pnnext</code>. I tried using both CSS selectors as well as xPath using several different methods to perform the click. They are outlined below:</p>
  
  <pre class="lang-js prettyprint-override"><code>var casper = require('casper').create({
      verbose: true,
      logLevel: "debug",
      waitTimeout: 20000
  });
  
  casper.defaultWaitForTimeout = 20000
  
  var x = require('casper').selectXPath;
  
  var config = {
      url: 'https://www.google.co.uk/search?site=&amp;source=hp&amp;q=house&amp;oq=house&amp;gs_l=hp.3..35i39l2j0l2j0i3j0l5.136980.137617.0.137690.7.7.0.0.0.0.106.450.3j2.5.0.starcytweb...0...1.1.62.hp..4.3.302.0.IK858rPmk0I'
  };
  
  casper.start(config.url);
  
  /* INSERT CODE HERE */
  
  casper.then(function() {
      this.capture('screenshot.png');
      console.log('New location is ' + this.getCurrentUrl());
  
      this.page.close();
      this.exit();
  });
  
  casper.run();
  </code></pre>
  
  <p>Attempt #1</p>
  
  <pre class="lang-js prettyprint-override"><code>casper.thenClick("#pnnext");
  </code></pre>
  
  <p>Attempt #2:</p>
  
  <pre class="lang-js prettyprint-override"><code>casper.then(function(){
      this.evaluate(function() {
          document.getElementById('pnnext').click();
      });
  });
  </code></pre>
  
  <p>Attempt #3:</p>
  
  <pre class="lang-js prettyprint-override"><code>casper.then(function(){
      this.evaluate(function() {
          document.querySelector('#pnnext').click();
      });
  });
  </code></pre>
  
  <p>Attempt #4 (times out):</p>
  
  <pre class="lang-js prettyprint-override"><code>casper.waitForSelector("#pnnext", function(){
      this.click('#pnnext');
  }, function(){ console.log("Time out"); }, 20000);
  </code></pre>
  
  <p>Attempt #5:</p>
  
  <pre class="lang-js prettyprint-override"><code>casper.then(function(){
      this.click(x('//*[@id="pnnext"]'));
  });
  </code></pre>
  
  <p>Just a side note, I've also tried executing this <a href="https://github.com/n1k0/casperjs/blob/master/samples/googlepagination.coffee" rel="nofollow">sample code </a> to no avail. Am I doing something wrong?</p>
  
  <hr>
  
  <p><code>phantomjs -&gt; v1.9.8</code></p>
  
  <p><code>casperjs -&gt; v1.1.0-beta3</code></p>