3

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

asked May 2, 2018 at 5:52
1
  • How to achieve this using css? Commented Feb 27, 2019 at 11:20

2 Answers 2

7

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.

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered May 2, 2018 at 6:01
0
5

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.

answered May 2, 2018 at 7:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.