How to convert Xpath to CSS

后端 未结 4 383
长发绾君心
长发绾君心 2020-12-17 03:33

My xpath is: /html/body/div/table/tbody/tr[2]/td[4]

I need to get an CSS to use it in jsoup selector.

I found a comparison between xpath and css

4条回答
  •  天命终不由人
    2020-12-17 04:22

    Works good for me.

    //Author: Oleksandr Knyga
    function xPathToCss(xpath) {
        return xpath
            .replace(/\[(\d+?)\]/g, function(s,m1){ return '['+(m1-1)+']'; })
            .replace(/\/{2}/g, '')
            .replace(/\/+/g, ' > ')
            .replace(/@/g, '')
            .replace(/\[(\d+)\]/g, ':eq($1)')
            .replace(/^\s+/, '');
    }
    

提交回复
热议问题