How to stop insert implicit tags when using `Copy

2019-07-28 17:21发布

I use Copy Xpath function in Chrome developer tools for nokogiri xpath parser.

But like this question, Chrome and Firefox's Developer tools insert tags like <tbody> implicitly.

Is there a way to get "real" xpath that I can use for nokogiri xpath parser?

1条回答
我命由我不由天
2楼-- · 2019-07-28 18:11

When you extract XPath from a browser, you do that form the actual DOM, where it is too late to know whether the <tbody> element was there or whether it was added implicitly.

You can replace all instances of /tbody/ with // so you don't care which case is it:

xpath = '//html/body/p/table/tbody/tr/td[2]/table/tbody/tr[2]'

xpath.gsub('/tbody/', '//')
# => "//html/body/p/table//tr/td[2]/table//tr[2]"
查看更多
登录 后发表回答