Find web element (button) by class and Name

I am very new to Selenium.

I am trying to find button Validate (see below) and not to use xpath.

<div class="order">
<a style="float: left;" href="#143207" class="VoidDoc ui-button ui-corner-all ui-widget" role="button"> Void </a>
<a style="float: left;" href="validatedocumentation.php?documentId=143207&amp;origin=viewreferral.php&amp;OrderId=836829" class="ui-button ui-corner-all ui-widget" role="button"> Validate </a>
<a class="signDocBehalf ui-button ui-corner-all ui-widget" style="float: left;" href="#143207#20263#836829" role="button">Sign</a>
<a style="float: left;" href="editdocumentation.php?documentId=143207&amp;origin=viewreferral.php" class="ui-button ui-corner-all ui-widget" role="button"> Edit </a>

I tried to look by a class name (unfortunately not unique) and text of the button (“Validate”):

.....Driver.findElement(By.xpath("//a[@class='ui-button ui-corner-all ui-widget' and text()='Validate']")).click();

also I tried

.....Driver.findElement(By.xpath("//div[@class='ui-button ui-corner-all ui-widget' and text()='Validate']")).click();

getting an error:

no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='ui-button ui-corner-all ui-widget' and text()='Validate']"}
  (Session info: chrome=94.0.4606.81)

I am guessing it is because "<a>" is nested in "<div>" , correct me if I am wrong.

How it would be right to identify this button.

Please, give me your kind advice.


You were almost correct.

the xpath that you are using

//a[@class='ui-button ui-corner-all ui-widget' and text()='Validate']

is partially correct. There are trailing spaces for text Validate, so you’d have to use contains

Use this xpath :

//a[@class='ui-button ui-corner-all ui-widget' and contains(text(),'Validate')]

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Since you’ve mentioned you do not wanna use xpath, not sure why though ?

If that is the case then please use css selector like this :