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

Time:09-15

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 >
    <a  href="/tasks">Todo List</a
    ><button
      
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span ></span>
    </button>
    <div  id="navbarSupportedContent">
      <ul >
        <li ><a  href="/">Home</a></li>
        <li ><a  href="/tasks">Tâches</a></li>
        <li ><a  href="/">Déconnexion</a></li>
      </ul>
    </div>
  </nav>
  <div ></div>
</div>
<div>
  <h2>Liste des tâches</h2>
  <div >Aucune tâche n'a encore été créée.</div>
  <hr />
  <h2>Créer une nouvelle tâche</h2>
  <div >
    <div >
      <label>Nom de la tâche</label
      ><input type="text"  value="" />
    </div>
    <div >
      <label>Description de la tâche en une ligne</label
      ><input type="text"  value="" />
    </div>
    <div >
      <br /><button >Ajouter la tâche</button>
    </div>
  </div>
</div>

CodePudding user response:

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]

CodePudding user response:

By xpath, for example in Firefox

Right click on page -> Select (Inspect Element), pick an element from the page, then right click on highlighted html -> Copy -> Xpath

Check https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/

  • Related