How can I find an element by CSS class with XPath ?

How can I find an element by CSS class with XPath ?

Asked on December 18, 2018 in XML.
Add Comment


  • 3 Answer(s)

    To find element by CSS class with Xpath:

         The selector must work and will be more efficient if it replace with correct markup.

    //*[contains(@class, 'Test')]
    

    div is a sought element,

    //div[contains(@class, 'Test')]

    This will match cases like class=”Testvalue” or class=”newTest”.

    //div[contains(concat(' ', @class, ' '), ' Test ')]
    

    Certainly it will match correctly and also use the normalize-space function to clean up stray whitespace characters around the class name.

    //div[contains(concat(' ', normalize-space(@class), ' '), ' Test ')]
    

    In all the versions, the * should be replaced by element name that actually match, or search each and every element in the document for the given condition.

    Answered on December 18, 2018.
    Add Comment

    Simple way:

    //div[@class="Test"]
    
    

    Let us assume <div class=”Test”> .

    Answered on December 18, 2018.
    Add Comment

    Try this to find element by CSS class:

    //div[contains(concat(' ', @class, ' '), ' Test ')]
    
    Answered on December 18, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.