0

I don't want to use xpath to map these elements, as we can see the class is the same for everyone. How to do please?

<label class="sc-fzowVh juqfMo">CNPJ:*</label>
<label class="sc-fzowVh juqfMo">CPF: (do usuário)*</label>

xpath:

//*[@id="boxCadastro"]/div/div[2]/div[1]/div/label
//*[@id="boxCadastro"]/div/div[2]/div[2]/div/label

See the image please: enter image description here

sabnola
  • 5
  • 3
  • An id should be unique per document. So, this is bad markup. But Capybara can find things by class or id; it just won't work well in your case because of the mistaken use of the ID element. – Todd A. Jacobs Mar 15 '21 at 15:05
  • Exactly why I am looking for help !! .. Can you help me ?? – sabnola Mar 15 '21 at 15:36

1 Answers1

0

Without seeing the HTML surrounding each of those label elements in order to be able to use scoping, the only option is to go with text matching on the label contents

element :label1, :css, 'label', text: 'CNPJ:*'
element :label2, :css, 'label', text: 'CPF: (do usuário)*'

or using the :label selector type

element :label1, :label, 'CNPJ:*'
element :label2, :label, 'CPF: (do usuário)*'

although having label elements without for attributes is generally a bad practice and negatively affects page accessibility, so you may want to talk to your developers about improving their HTML

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78