How to convert Xpath to CSS

后端 未结 4 372
长发绾君心
长发绾君心 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:00

    One should learn how to write css selectors, but a for a quick fix, try: cssify

    For example, I put in your xpath and it spit out: html > body > div > table > tbody > tr:nth-of-type(2) > td:nth-of-type(4)

    Try it out.

    0 讨论(0)
  • 2020-12-17 04:13

    Are you looking for something like this:

    http://jsfiddle.net/YZu8D/

    .tablecont tr:nth-child(2) td:nth-child(4) {background-color: yellow; }
    .tablecont tr:nth-child(3) td:nth-child(4) {background-color: yellow; }
    
    0 讨论(0)
  • 2020-12-17 04:19

    While an expression like (//E)[2] can't be represented with a CSS selector, an expression like E[2] can be emulated using the :nth-of-type() pseudo-class:

    html > body > div > table > tbody > tr:nth-of-type(2) > td:nth-of-type(4)
    
    0 讨论(0)
  • 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+/, '');
    }
    
    0 讨论(0)
提交回复
热议问题