Since you are writing your test scripts in Java with Selenium,
why can't you simply use the .click()
method of Selenium to click the button rather than using a JavaScript Executor?
WebElement continueButton = driver.findElement(By.xpath("//span[text()='CONTINUE']"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
continueButton.click();
This will simply get your job done!
Or even this single line would do it:
driver.findElement(By.xpath("//span[text()='CONTINUE']")).click();
Edit:
You can also try by clicking the button with the Xpath of the <button>
tag rather than the <span>
tag enclosed within.
Here's my HTML,
<!DOCTYPE html>
<html>
<head>
<style>
button {border: solid 1px #000; height: 40px; background: #000; color: #fff;}
button:hover {border: solid 1px #000; height: 40px; background: #fff; color: #000;}
</style>
</head>
<body>
<button
class="MuiButtonBase-root MuiButton-root jss47 jss58 MuiButton-contained jss48 jss59 MuiButton-containedPrimary MuiButton-disableElevation"
tabindex="0" type="button">
<span class="MuiButton-label jss49 jss60">CONTINUE</span>
</button>
<!--<a href="http://localhost/1/test.txt" target="_blank">Click Here</a>-->
</body>
</html>
And my Selenium script,
try {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "//geckodriver");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "FFLogs.txt");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://localhost/1");
driver.manage().window().maximize();
Thread.sleep(2000);
WebElement continueButton = driver.findElement(By.xpath("//button[@tabindex='0']"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
continueButton.click();
Thread.sleep(2000);
driver.findElement(By.xpath("//button[@tabindex='0']")).click();
Thread.sleep(5000);
driver.close();
//driver.quit();
} catch (Exception e) {
e.printStackTrace();
}