Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath - select element that contains text in CLASS name [duplicate]

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.

like image 597
Matthewek Avatar asked Nov 19 '25 11:11

Matthewek


1 Answers

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")]
like image 160
Tobi Avatar answered Nov 21 '25 09:11

Tobi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!