How to stop insert implicit tags when using `Copy Xpath` in Chrome Developer tools

自古美人都是妖i 提交于 2019-12-24 07:36:05

问题


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:


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]"


来源:https://stackoverflow.com/questions/23311836/how-to-stop-insert-implicit-tags-when-using-copy-xpath-in-chrome-developer-too

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!