As you are able to locate the following element:
<button class="signup-button b green">Get Started!</button>
using:
driver.findElement(By.cssSelector("button.signup-button")).click();
but was unable to locate the same element using:
driver.findElement(By.className("signup-button")).click();
That's because there were other elements which renders within the HTML DOM with className as signup-button
even before the desired element, which possibly may be invisible by design.
Ideally, you should also be able to use a xpath based Locator Strategy:
driver.findElement(By.xpath("//button[@class='signup-button' and text()='Get Started!']")).click();
Best Practices
But as per best practices, you need to use WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.signup-button"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='signup-button' and text()='Get Started!']"))).click();
References
You can find a couple of relevant discussions in: