xpath - select element that contains text in CLASS name

10,973

Your statement is almost correct. You only are missing the SelectAll (*) at the beginning of your Statement. Just add it, and the XPath should work:

//*[contains(@class, "snackbar-info")]
Share:
10,973
Matthewek
Author by

Matthewek

I am android game developer, java programmer, from Poland, currently live in UK. Author of android games downloaded over 30.000.000 times, currently automation engineer.

Updated on June 05, 2022

Comments

  • Matthewek
    Matthewek almost 2 years

    I am trying to find a way to select this particular web element:

    <snack-bar-container class="mat-snack-bar-container ng-tns-c43-25 ng-trigger ng-trigger-state snackbar-info mat-snack-bar-center ng-star-inserted" role="alert" style="transform: translateY(0%);"><!----><simple-snack-bar class="mat-simple-snackbar ng-tns-c44-26 ng-trigger ng-trigger-contentFade ng-star-inserted" style="">Asset successfully loaded! <!----><button class="mat-simple-snackbar-action ng-tns-c44-26 ng-star-inserted" style="">Close</button></simple-snack-bar></snack-bar-container>
    

    Currently I was selecting that by text:

    .//snack-bar-container//*[contains(text(),'There are errors on the page:')]
    

    But because there will be localisation (translation of texts) it will be unreliable in a long term, question is, is how to select element, which has a class attribute that contains some specific text in class name.

    As you can see my web element class is very long, and often varies, so I need to check if it contains some specific text, I tried with:

    .//[contains(@class,’snackbar-info’)]
    

    But it does not work, any suggestions? Thank you.

    • Andersson
      Andersson over 5 years
      I see no elements with text "There are errors on the page"'... Update your HTML sample
    • Matthewek
      Matthewek over 5 years
      Hi @Andersson it is child of the element that I pasted in the first post, but for this question I thought it is not relevant, because question is how to select element that has class name that contains some text, thank you