Selector Casperjs [Caspererror]

Asked

Viewed 96 times

1

I want to do a test using Casperjs. I want him to click the login button.

I have the following menu:

<ul id="menu-top">
<li class="0 first"><a title="" href="/empresa">Empresa</a></li>
<li class="1"><a title="" href="/plano-de-negocio">Plano de Negócio</a></li>
<li class="2"><a title="" href="/produtos">Produtos</a></li>
<li class="3"><a title="" href="/contato">Contato</a></li>
<li class="4 last"><a title="" href="login.php">Área restrita</a></li>
</ul>

To click I used the following code:

var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});
casper.start('http://localhost/teste');

casper.then(function(){
    this.click('.4.last>a');
});

casper.run();

But the following error returns to me:

[error] [remote] findAll(): invalid selector provided ". 4.last>a":Error: SYNTAX_ERR: DOM Exception 12

Caspererror: Cannot Dispatch mousedown Vent on nonexistent selector: . 4.last>a

I’m using firepath to find the css path of the button (.4.last>a)

  • Maybe he didn’t like that it starts with number. Tried li.4.last>a?

  • Yes, I tried that too. this.click('li.4.last>a');

2 answers

2


I managed to solve my problem by indicating that I would insert an Xpath. Thus:

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

casper.then(function(){
    this.click(x(".//*[@id='menu-top']/li[5]/a"));  
});

1

Just to complement... Another simple way is to use the clickLabel():

this.clickLabel('Área restrita', 'a');

or adjust the CSS Path to:

'#menu-top > li:nth-child(5) > a'

or a third possibility:

'a[href="login.php"]'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.