Selenium how to get the elements who doesn't have a name or id

Viewed 35

i want to find the two inputs using selenium using findElement by css i want to fill in the two inputs, i was only able to find one input with "input[type='text']" and im not sure how to get the other one please check my html

  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="/tasks">Todo List</a
    ><button
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item"><a class="nav-link" href="/">Home</a></li>
        <li class="nav-item"><a class="nav-link" href="/tasks">Tâches</a></li>
        <li class="nav-item"><a class="nav-link" href="/">Déconnexion</a></li>
      </ul>
    </div>
  </nav>
  <div class="container mt-5"></div>
</div>
<div>
  <h2>Liste des tâches</h2>
  <div class="alert alert-info">Aucune tâche n'a encore été créée.</div>
  <hr />
  <h2>Créer une nouvelle tâche</h2>
  <div class="row">
    <div class="col">
      <label>Nom de la tâche</label
      ><input type="text" class="form-control" value="" />
    </div>
    <div class="col">
      <label>Description de la tâche en une ligne</label
      ><input type="text" class="form-control" value="" />
    </div>
    <div class="col">
      <br /><button class="btn btn-primary">Ajouter la tâche</button>
    </div>
  </div>
</div>

3 Answers

You can either use XPath, and to find it easily open Inspect Element, right click on input, click on Copy XPath,

or you can get all matching elements and select it from the list, like
findElements("input[type='text']")[0]
findElements("input[type='text']")[1]

If you found the first input element, you can simply do the same you did to find it but use find_elements() instead of find_element().
It will return a list with the two inputs.

Related