0

I'm trying to click below button.

<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>

I used below code to click it.

WebElement continueButton = driver.findElement(By.xpath("//span[text()='CONTINUE']"));
Assert.assertTrue(continueButton.isDisplayed());
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
js.executeScript("arguments[0].click();", continueButton);

Even though no exception given button was not clicked and script is not executed completely.

Can someone give me a solution ?

2 Answers 2

1

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();
}
2
  • I tried both ways and didn't work. Jun 3, 2022 at 8:25
  • I've edited my answer with the solution I've tried and ensured it's working. Check if this works for you. If not, it would be good if you can possibly share the complete DOM/HTML of the page so that I can try to debug the issue. Jun 3, 2022 at 8:46
0

Issue resolved after adding a button availability check and increasing wait time.

WebElement continueBtn = driver.findElement(By.xpath("//span[text()='CONTINUE']"));
        
        if(continueBtn.isDisplayed()) {
            
            try {
                Thread.sleep(8000);
                System.out.println("Waiting time 6s");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            js.executeScript("arguments[0].click();", continueBtn);
        
        }else {
            System.out.println("Button not found");
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.