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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to click a button in selenium with java without name or ID without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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!

Answer

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();
We are here to answer your question about How to click a button in selenium with java without name or ID - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji