How do I find a 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 buttons: Confirm and the other is Cancel I can find a button with XPath, but I don't want to use XPath. Is there any other way to find the button element in this case?
I've tried this:
driver.findElement(By.className("btn dialog-confirm btn-primary")).click();
He didn't find the button Thank you for your help.
+3
source to share
4 answers
Just check one class dialog-confirm
:
driver.findElement(By.className("dialog-confirm")).click();
Or use CSS Selector
:
driver.findElement(By.cssSelector("button.dialog-confirm")).click()
+3
source to share
Added alecxe's answer and master slave. It would be more specific if the button text was clicked, which is also easier to understand. Find snippet for button click using xpath below.
driver.findElement(By.xpath("//button[text()='Confirm']")).click();
driver.findElement(By.xpath("//button[text()='Cancel']")).click();
+2
source to share