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
-
How to achieve this using css?Bharati Belgaonkar– Bharati Belgaonkar2019年02月27日 11:20:23 +00:00Commented Feb 27, 2019 at 11:20
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.
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.
Explore related questions
See similar questions with these tags.