1

i am learning xpath, and i am trying to get some data from html usint xpath

i found that google chrome has a option to "copy xpath" and it works nice

but doesnt work to this exemple

<div id="site-main">
    <header class="main" role="banner">some divs      </header>
</div>

i use this on google chrome console

$x("//*[@id="site-main"]/header")

and return "SyntaxError: Unexpected identifier" i dont see anythin wrong...do you?

2 Answers2

3
$x("//*[@id="site-main"]/header")
            ^         ^

The marked quotes cause the error — in fact, the string is terminated right after =.

You have to escape the quotes inside the XPath expression. The way of escaping depends on the language you are using. If it's Javascript, then it would be with \:

$x("//*[@id=\"site-main\"]/header")

Also have a look on this question: Escape quotes in JavaScript

Community
  • 1
  • 1
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
2

You can use single quotes in the xpath query:

$x("//*[@id='site-main']/header")
hek2mgl
  • 152,036
  • 28
  • 249
  • 266