Question

I am new to using Casperjs and I am trying to get this script to copy some text but it is unable to find the search button even though I have the right path. I have been able to click all buttons in the script before but it gives the error at the line:

    casper.wait(2000, function () {
        casper.click(x('//*[@id="SRCHBTN"]'));     <----Error here
        casper.capture('CurrentScreen.png');
    });


CasperError: Cannot dispatch mousedown event on nonexistent selector:

Here is the code:

var casper = require('casper').create();

var x = require('casper').selectXPath;

casper.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');

casper.start('url');

casper.then(function () {
    this.sendKeys('user', 'user');
    this.sendKeys('pswd', 'pass');
    console.log('Entering Data');

});

casper.thenClick(x('//*[@id="login"]/table[1]/tbody/tr[3]/td[2]/input'), function () {
    console.log('Logging in..');
});

casper.wait(2000, function () {
    casper.click(x('//*[@id="SRCH_LINK"]/a'));
});

casper.wait(2000, function () {
    casper.click(x('//*[@id="SRCHBTN"]'));
    casper.capture('CurrentScreen.png');
});






casper.wait(4000, function () {
    casper.click(x('//*[@id="TITLE_HL$0"]'));
casper.wait(2000, function() {

    //this is pop up window section

    casper.waitForPopup(/popup\.html$/, function () {
        this.test.assertEquals(this.popups.length, 1);
    });

    // this will set the popup DOM as the main active one only for time the
    // step closure being executed
    casper.withPopup(/popup\.html$/, function () {
        this.test.assertTitle('Job Details - Google Chrome');
        var targetText = casper.fetchText('#DESCR');
        console.log(targetText);
    });

    // next step will automatically revert the current page to the initial one
    casper.then(function () {
        this.test.assertTitle('Main page title');
    });





    });
});


casper.run();
Était-ce utile?

La solution

If the existence of expected element depends on an asynchronous call, waiting for 2 seconds will not guarantee the call is completed. Instead of wait() you should use the waitForSelector() function:

casper.waitForSelector(x('//*[@id="SRCHBTN"]'), function () {
    casper.click(x('//*[@id="SRCHBTN"]'));
    casper.capture('CurrentScreen.png'); });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top