XPath to find elements that does not have an id or class

后端 未结 4 1170
时光取名叫无心
时光取名叫无心 2021-01-30 01:26

How can I get all tr elements without id attribute?

...
...
...

Than

相关标签:
4条回答
  • 2021-01-30 01:58

    Pretty straightforward:

    //tr[not(@id) and not(@class)]
    

    That will give you all tr elements lacking both id and class attributes. If you want all tr elements lacking one of the two, use or instead of and:

    //tr[not(@id) or not(@class)]
    

    When attributes and elements are used in this way, if the attribute or element has a value it is treated as if it's true. If it is missing it is treated as if it's false.

    0 讨论(0)
  • 2021-01-30 02:00

    Can you try //tr[not(@id)]?

    0 讨论(0)
  • 2021-01-30 02:06

    If you're looking for an element that has class a but doesn't have class b, you can do the following.

    //*[contains(@class, 'a') and not(contains(@class, 'b'))]
    

    Or if you want to be sure not to match partial.

    //*[contains(concat(' ', normalize-space(@class), ' '), ' some-class ') and 
    not(contains(concat(' ', normalize-space(@class), ' '), ' another-class '))]
    
    0 讨论(0)
  • 2021-01-30 02:10
    if (elm.hasAttribute('id')) { 
    //if id - implement here
        } else if (elm.hasAttribute('class')) { 
            //if class - implement here
        } else { 
            for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) { 
                if (sib.localName == elm.localName)
                    i++;
            }; 
            segs.unshift(elm.localName.toLowerCase() + '[' + i + ']'); 
        }
    
    0 讨论(0)
提交回复
热议问题