-1

How can I use xpath instead of id to find the element in DOM. I know that for id I can use: $("#id")[0].

I use $("#id")[0] inside Developer mode in browser in order to get the element by id to see what methods (like .getText(), innerHTML or others) am I able to use for the element. I want to know how to do this by XPATH

Thanks in advance

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
AndreeaFed
  • 38
  • 6
  • I use this inside Developer mode in browser in order to get the element by id to see what methods (like .getText() , innerHTML or others) am I able to use for the element. I want to know how to do this by XPATH – AndreeaFed Aug 03 '16 at 11:53
  • AFAIK, there is no embedded methods in `Javascript` to use `XPath` – Andersson Aug 03 '16 at 12:00
  • @Andersson Actually, there is a method: $x("xpath") – AndreeaFed Aug 03 '16 at 12:58

2 Answers2

1

On Firefox console you can find (and explore) the element like this:

$x("//button[@id='myButton']")

But if you want to call a function on the element, you have to call it like this:

$x("//button[@id='myButton']")[0].click()

because there is always an array of elements returned (provided that the element is present and the first one in the array).

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
0

Thanks for the comments. I found out the answer. It can be done with:

$x("xpath")
AndreeaFed
  • 38
  • 6