How to click a button in selenium with java without name or ID

How can i use selenium to click this button?

 <button type="submit" class="button--primary button button--icon button--icon--login"><span class="button-text">Log in</span></button>

I have tried these

    driver.findElement(By.className("button--primary button button--icon button--icon--login")).click();
    driver.findElement(By.xpath("//button[contains(@class='button--primary button button--icon button--icon--login')]")).click();
    driver.findElement(By.xpath("//span/button[text()='Log in' and @class='button']")).click();
    driver.findElement(By.xpath("//span/button[text()='Log in'][1]")).click();

but no avail, help!


Just looks like you almost had it with your last try.

This should work though:

driver.findElement(By.xpath("//button//span[text()='Log in']")).click();

When you use contains you should have a comma not an equals after the @class. Your own path would’ve worked in that case:

driver.findElement(By.xpath("//button[contains(@class,'button--primary button button--icon button--icon--login')]")).click();

Leave a Reply

Your email address will not be published. Required fields are marked *