ProgramingTip

ID 또는 클래스가없는 요소를 찾기위한 XPath

bestdevel 2020. 10. 21. 21:13
반응형

ID 또는 클래스가없는 요소를 찾기위한 XPath


id 속성이없는 모든 tr 요소를 어떻게 사용할 수 있습니까?

<tr id="name">...</tr>
<tr>...</tr>
<tr>...</tr>

감사합니다


꽤 직설적 인 :

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

그러면 속성 tr이 모두없는 모든 요소가 제공 됩니다. 둘 중 하나가없는 모든 요소 를 원하면 대신 사용하십시오 .idclasstrorand

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

이런 식으로 속성과 요소를 사용할 때 속성이나 요소에 값이 있으면 참인 것처럼 처리합니다. 누락 된 경우 거짓 인 것처럼 처리됩니다.


당신이 요소를 소유하고있는 클래스는 a하지만 하지 않는 클래스가 b다음을 수행 할 수 있습니다.

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

또는 부분 일치하지 않습니다.

//*[contains(concat(' ', normalize-space(@class), ' '), ' some-class ') and 
not(contains(concat(' ', normalize-space(@class), ' '), ' another-class '))]

너 시도 할 수있어 //tr[not(@id)]?


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 + ']'); 
    }

참고 URL : https://stackoverflow.com/questions/2404130/xpath-to-find-elements-that-does-not-have-an-id-or-class

반응형