I have three buttons withing a div. All three buttons belong to same class (class names are same). What I want to do is capture each button using its text (Buttons have different texts) and xpath of the div.

If it's possible to find buttons separately using button's class name and text I'm OK with that too.
How can I perform such task?
I'm using Java and Testng


2 Answers 2


Use below command for finding the button via text:

driver.findElementsByXPath("//button[contains(text(),'Text in your webpage')]");

This will work to capture all the 3 buttons with the same class name.

  • It worked. Thanks. I will accept the answer
    – Syrus
    Commented May 2, 2018 at 6:13

Using contains in xpath, for me is tricky. Tests are flaky with this approach. The better way is to use:

//button[text()='Some text']

Now, you compare/test the text of the button with expected text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.