0

When I run the following code, I am unable to find any element by an id when trying to assign a value to 'element'. If I run the code in the browser console, I get back an assignment.

Edited Question:So I added the casper.evaluate method but I'm still getting a null response.

phantom.casperTest = true;
casper.test.begin('assertExists() tests', 2, function suite(test) {
    casper.start('http://www.google.com', function() {
      test.assertTitleMatches(/Google/);
      var element = casper.evaluate(function() {
        var f = document.getElementById('tsf');//this line is returning null
        return(JSON.stringify(f));
      });
      casper.echo("element is:" + element);
      this.test.assertExists( element, 
         'the element exists'
         );
     }).run(function() {
        test.done();
    });
});
jmscholen
  • 23
  • 1
  • 5
  • Possible duplicate of [Why doesn't plain JavaScript work in CasperJS?](http://stackoverflow.com/questions/18328387/why-doesnt-plain-javascript-work-in-casperjs) – Artjom B. Jan 07 '16 at 09:33
  • I edited my code per the comments of other questions that suggested the use of casper.evaluate() but I am still getting a null value as a response. – jmscholen Jan 07 '16 at 14:47
  • Google pages are usually different for different browsers. PhantomJS is different from all of your desktop browsers. You could try to increase the viewport size and change the user agent string. I don't know what you want to do, but even if `#tsf` where available in the page, `return(JSON.stringify(f));` wouldn't give you anything useful. What do you want to achieve? – Artjom B. Jan 07 '16 at 14:49
  • This is more of a learning exercise. Here, I'm wanting to locate the element and determine that it exists. – jmscholen Jan 07 '16 at 15:40
  • Then just use another site. Google pages are not the best to start with experimenting with CasperJS. – Artjom B. Jan 07 '16 at 15:43

0 Answers0