XPath

Its little tricky but believe me its not that hard to find.

Follow below steps to get XPath

  1. Right click any button or link or whaterver you want to click in webpage and select Inspect.
  2. It will open developer console and highlight that element under Elements Tab
  3. Right click that element and select Copy > Copy XPath.
  4. Thats it you have your XPath of that element now.

Examples

//div/a[1]

<div>
  <a>One</a> 👈 selected
  <a>Two</a>
</div>

//div/a[last()]

<div>
  <a>First</a>
  <a>Middle</a>
  <a>Last</a> 👈 selected
</div>

//div/a[last()-1]

<div>
  <a>First</a>
  <a>Middle</a> 👈 selected
  <a>Last</a>
</div>

//div/a[position()>2]

<div>
  <a>First</a>
  <a>Second</a>
  <a>Third</a> 👈 selected
  <a>Forth</a> 👈 selected
</div>

//a[@href]

<div>
  <a>First</a>
  <a>Second</a>
  <a href="#">Third</a> 👈 selected
  <a>Forth</a>
</div>

//a[@href='google']

<div>
  <a>First</a>
  <a href="#">Second</a>
  <a href="google">Third</a> 👈 selected
  <a>Forth</a>
</div>

//button[@id='1']

<div>
  <a>First</a>
  <button type="button" id="1">Click me</button> 👈 selected
  <a href="#">Second</a>
  <a href="dhruv-techapps.github.io">Third</a>
  <a>Forth</a>
</div>

//a[@id<'3']

<div>
  <a id='1'>Primary</button> 👈 selected
  <a id='2'>Secondary<button> 👈 selected
  <a id='3'>Warning<button>
  <a id='4'>Me Too<button>
</div>

//button[contains(@class,'me')]

<div>
  <button type='button' id='1'>Primary</button>
  <button class='me second'>Secondary<button> 👈 selected
  <button class='me warning'>Warning<button> 👈 selected
  <button class='me-too done'>Me Too<button> 👈 selected
</div>

//button[contains(text(),'Me')]

<div>
  <button type='button' id='1'>Primary</button>
  <button class='me second'>Secondary<button>
  <button class='me warning'>Warning<button>
  <button class='me-too done'>Me Too<button> 👈 selected
</div>

//*[substring(text(), 0, 3)>='50']

<div>
  <span class='bdage'>55 %</span> 👈 selected
  <span class='bdage'>50 %</span> 👈 selected
  <span class='bdage'>45 %</span>
  <span class='bdage'>43 %</span>
</div>

Inbox me if you find difficult to find XPath or you have any better suggestion for me :)