kirk douglas kirk douglas - 2 months ago 11
Java Question

How to find button element with webdriver?

i have the following code for a button :

<div class="buttons">
<button class="btn dialog-confirm btn-primary" style="margin-left: 4px;">Confirm</button>
<button class="btn dialog-cancel" style="margin-left: 4px;">Cancel</button>
</div>


there are two button on is Confirm and another is Cancel
I can find the buttob with xpath but i don't want to use xpath.
Is there another way to find the button element in this case?

i tried this:

driver.findElement(By.className("btn dialog-confirm btn-primary")).click();


it did not find the button
Thank you for your help

Answer

Just check for a single dialog-confirm class:

driver.findElement(By.className("dialog-confirm")).click();

Or, use a CSS Selector:

driver.findElement(By.cssSelector("button.dialog-confirm")).click()
Comments