XPath
1.
Chrome和傲游浏览器的Console中执行的Xpath代码有些区别。
例如,要提取a tag中的某个属性,在傲游中是这样写:
$x('//a/@href')
而在Chrome中则是
$x('//a')[0].getAttribute('href')
2:// 和 /
在XPath中,双斜线 "//" 表示收集在Hierarchy中任意位置的某类型的元素。比如 “//a” 就表示此文件中的所有 a tag。
而单斜线 "/" 则表示属于某一个tag下一级的tag,比如 "//div/p" 就表示直接位于所有div下一级的p tag。
3:*
用 '*' 来选择某一层下的所有元素.
例:$x('//div/*')
4:select tags with consitions
$x('//a[@href]') # a tags with href attribute
$x('//a[@href="http://www.iana.org/domains/example"]') # a tags whose href is as above
$x('//a[contains(@href, "iana")]') # a tags whose href contains "iana"
$x('//a[starts-with(@href, "http://www.")]') # a tags whose href start with "http://www."
$x('//a[not(contains(@href, "abc"))]') # a tags whose href do not contain "abc"
5 更多XPath命令请见
http://www.w3schools.com/xsl/xsl_functions.asp