I'm trying to click the BUTTON in the HTML below,
<div class="row" id="signin-button-container">
<button class="x2-button x2-blue" id="signin-button" style="border-color: #8d8d8d;;background: rgb(151, 151, 151); background: -moz-linear-gradient(top, #a1a1a1, #8d8d8d); background: -webkit-linear-gradient(top, #a1a1a1, #8d8d8d); background: -o-linear-gradient(top, #a1a1a1, #8d8d8d); background: -ms-linear-gradient(top, #a1a1a1, #8d8d8d); background: linear-gradient(top, #a1a1a1, #8d8d8d);"> Sign in </button>
<div class="clearfix"></div>
</div>
I have tried all the selectors including the following:
WebDriverWait wait3 = new WebDriverWait(driver, Duration.ofSeconds(3));
WebElement signInButton;
try {
signInButton = wait.until(ExpectedConditions.elementToBeClickable(By.tagName("button")));
System.out.println("Sign-in button successfully located.");
signInButton.click();
} catch (Exception e) {
System.out.println("Failed to locate the sign-in button.");
e.printStackTrace(); // Print exception details for debugging
return null; // Return null to indicate failure
}
I get "Sign-in button successfully located" consoled but still the button doesn't get clicked.
I'm assuming you ARE getting an error message. You check the logs and you get a message, "Sign-in button successfully located.", then a message, "Failed to locate the sign-in button.", and THEN the actual exception message printed to the logs. I'm assuming the error message is an element not interactable exception.
There are a number of issues that should be addressed to make this work and work better,
You don't get any error when you try to click because you are catching all exceptions
catch (Exception e)
A better practice is to only catch exceptions you are expecting and only if you plan on handling them. Eating all exceptions and printing a generic message is not very helpful when you go to debug... which you are experiencing right now. I would remove the try-catch
and let the script fail when it throws. Continuing the script after an exception will likely cause it to fail later and then you'll have a harder time figuring out why it failed.
Your locator, By.tagName("button")
, is way to generic. That's going to grab every BUTTON on the page, which depending on the page could be a LOT of BUTTONs. The HTML of the BUTTON you posted has an ID... you should always use that, By.id("signin-button")
. It's likely you are grabbing a BUTTON but the wrong button and it's not visible or otherwise causing a failure and that's why you get the success message and then the failure message.
This message, "Sign-in button successfully located." is not really true. It just prints when the wait doesn't time out... it doesn't mean that it's found the correct element, which I'm assuming is what is happening here.
My suggestion is to reduce the code to the below,
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
...
String url = "https://staging3.rolustech.com:44353/index.php/site/login";
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("LoginForm_username"))).sendKeys("username");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("LoginForm_password"))).sendKeys("password");
WebElement signInButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("LoginForm_password")));
signInButton.click();
Thread.sleep(1000);
signInButton.click();
If the code throws an exception, it will fail and get printed to the logs and you'll have the full exception message that you can review. Don't print success messages, they don't add any value. If your code gets past that line, you know it succeeded. Don't make yourself or others wade through hundreds of lines of success messages just to find the error message. It just prolongs the investigation time unnecessarily.
Other misc suggestions...
wait3
at the top of the code but you aren't actually using it. Three lines below you use wait
instead.