2

How do I locate my button that has the text Pesquisar > in my Selenium WebElement?

My HTML:

<div style="font-size: 10pt; font-weight: bold">Portais
</div>
<hr>
<form name="Form1" method="post">
  <input type="hidden" name="operacao" value="1">
  <input type="hidden" name="d" value="">
  <input type="hidden" name="equipe" value="">
  <input type="text" name="conteudo" maxlength="200" size="20" onkeyup="escolhe_opcao_pesquisa(this);">
  <select name="opcao_pesquisa">
    <option value="A">Ambiente</option>
    <option value="C">CNPJ</option>         
    <option value="E" selected="">Nome do Portal</option>
    <option value="P">Número do Portal</option>
    <option value="R">Rede</option>
    <option value="L">Release</option>
    <option value="S">Sufixo Login</option>         
  </select>
  <input type="button" value="Pesquisar &gt;">
</form>
<hr>
<form name="frm" method="post">
  <input name="portal" type="hidden" value="">
  <input name="deploy" type="hidden" value="">
  <input name="correcaoFFC" type="hidden" value="N">
  <input name="correcaoERP" type="hidden" value="N">
  <input name="correcaoPOS" type="hidden" value="N">
  <input name="correcaoJAN11" type="hidden" value="N">
  <input name="homologacao" type="hidden" value="N">
</form>

2 Answers 2

1

As you have a lot of inputs I suggest filtering for the type of the input, in this case for button. Then you can narrow down the buttons based on their value attribute.

In your case the xpath expression should look like this:

//form//input[@type='button' and contains(@value,'Pesquisar')]

You can use it to find an element by xpath from your test like:

driver.FindElement(By.XPath("//form//input[@type='button' and contains(@value,'Pesquisar')]"));
2
  • Small addition, xpath should be avoided if possible because it's the most complicated evaluation you can use to define an element. I'd recommend using cssSelector with "form[name='form1'] input[type='button']" since the browser is already optimized to evaluate css selectors. For reference: w3.org/TR/webdriver1/#xpath
    – Daniel
    Jan 22, 2019 at 8:10
  • @godisalie92 I think you could put it into an answer. :) Jan 22, 2019 at 9:54
1

I think you are more aiming towards finding a correct locator for the desired button. You might consider using the below xpath:

//form//input[contains(text(), 'Pesquisar')]

in findElement by xpath as the locating strategy.

1
  • I am not sure that this will work, text and value are not the same. Jan 5, 2019 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.