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

Scrub :

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!

cullzie :

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();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=143346&siteId=1