Need Help Troubleshooting Button Click Error

Hi All,

Can someone help me troubleshoot an error I encounter when clicking a button?

Error:
Caused by: org.openqa.selenium.ElementClickInterceptedException: element click intercepted

If I use ‘Enhanced Click’ instead of ‘Click’, the problem is fixed. However, the execution time increases significantly. How can I resolve this issue without compromising performance?

1 Like

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

Please note that I also tried adding ‘wait for element to be clickable’ before clicking the button, but the same error occurred.

I had a similar issue when I tried to click on a button using WebUI.click(...). My HTML was like:

<div>
     <input ...
     <button ...
<div>

I was able to get the button to click using Javascript.

Maybe like:
import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor
import com.kms.katalon.core.webui.common.WebUiCommonHelper as WebHelper

	/**
	 * Perform a click on Test Object
	 * @param element = our Test Object
	 * @param timeout (optional)
	 */
	@Keyword
	public static clickButtonJS(TestObject to, int timeout = 10)  {
		try {
			WebElement element = WebUI.findWebElement(to, timeout)
			WebUI.executeJavaScript("arguments[0].click()", Arrays.asList(element))
		} catch (Exception) {
			WebUI.comment("Could not click on Test Object")
		}
	}

	/**
	 * Perform a click on Web Element
	 * @param element = our Web Element
	 */
	@Keyword
	public static clickElementJS(WebElement element) {
		try {
			WebUI.executeJavaScript("arguments[0].click()", Arrays.asList(element))
		} catch (Exception) {
			WebUI.comment("Could not click on Web Element")
		}
	}

Edit: you can use WebHelper or WebUI for “findWebElement”. I just changed it to the latter.

Hi,

Why should we proceed with these additional JavaScript steps when we already have the capability to use Katalon with the recorder?

Edit: I am assuming you are using the web recorder of Katalon Studio, and not their other product, Katalon Recorder. If you are using Katalon Recorder, you would have to put the method into a function, which I will let you search this forum as others have done similar.

I have the javascript as a Keyword since I need to use it for all the buttons that are in the specific pattern I stated above, otherwise, my test case click fails. Basically, it was due to the HTML of the page I was working on. You can put the Keyword into your code, in-line if you want to try it. If you want to get past your Exception, I give you what I had to do.

Maybe someone else has a “better” option.

Edit2: you can create a method within your Test Case above your code and below the import statements and call it within your test case. You can copy and paste the method into the Script tab, and then “find and replace” your WebUI.click to clickButtonJS and see if it works. Back up your project beforehand!

import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable

	public clickButtonJS(TestObject to, int timeout = 10)  {
		try {
			WebElement element = WebUI.findWebElement(to, timeout)
			WebUI.executeJavaScript("arguments[0].click()", Arrays.asList(element))
		} catch (Exception) {
			WebUI.comment("Could not click on Test Object")
		}
    }

//blah blah blah

clickButtonJS(findTestObject('myPage/button_Login'), 10)
// or
clickButtonJS(findTestObject('myPage/button_Login'))

1 Like

I would try to answer to this question.

Let me translate the question: The Enhanced Click works fine, but it takes significantly long time. Why?


In the folder where you installed Katalon Studio, you would find the source code of com.kms.katalon.core.webui.keyword.builtin.EnhancedClickKeyword, which is the implementation of the “Enhanced Click” keyword.

See <Katalon installatio folder>/Contents/Eclipse/configuration/resources/source folder, where you will find several jar files. Please find a jar file com.kms.katlaon.core.webui/com.kms.katalon.core.webui-sources.jar. Unzip the jar file. You will find a Groovy source code com/kms/katalon/core/webui/keyword/builtin/EnhancedClickKeyword.groovy.

Here I quote the whole code:

package com.kms.katalon.core.webui.keyword.builtin

import java.text.MessageFormat

import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait

import com.kms.katalon.core.annotation.internal.Action
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.exception.StepFailedException
import com.kms.katalon.core.helper.KeywordHelper
import com.kms.katalon.core.keyword.internal.SupportLevel
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.trymonad.Try
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.constants.StringConstants
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.internal.WebUIAbstractKeyword
import com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain

import groovy.transform.CompileStatic

@Action(value = "enhancedClick")
class EnhancedClickKeyword extends WebUIAbstractKeyword {

    @CompileStatic
    @Override
    public SupportLevel getSupportLevel(Object ...params) {
        return super.getSupportLevel(params)
    }

    @CompileStatic
    @Override
    public Object execute(Object ...params) {
        TestObject to = getTestObject(params[0])
        FailureHandling flowControl = (FailureHandling)(params.length > 1 && params[1] instanceof FailureHandling ? params[1] : RunConfiguration.getDefaultFailureHandling())
        click(to,flowControl)
    }

    private void scrollToElement(WebDriver webDriver, WebElement webElement) {
        try {
            Actions builder = new Actions(webDriver);
            builder.moveToElement(webElement);
            builder.build().perform();
        } catch(Exception e) {
            logger.logError(e.getMessage());
        }
        try {
            ((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView(true);", webElement);
        } catch(Exception e) {
            logger.logError(e.getMessage());
        }
    }
    @CompileStatic
    public void click(TestObject to, FailureHandling flowControl) throws StepFailedException {
        WebUIKeywordMain.runKeyword({
            boolean isSwitchIntoFrame = false
            try {
                WebUiCommonHelper.checkTestObjectParameter(to)
                isSwitchIntoFrame = WebUiCommonHelper.switchToParentFrame(to)
                WebElement webElement = WebUIAbstractKeyword.findWebElement(to)
                WebDriver webDriver = DriverFactory.getWebDriver();
                int timeout = KeywordHelper.checkTimeout(RunConfiguration.getTimeOut())
                logger.logDebug(MessageFormat.format(StringConstants.KW_LOG_INFO_CLICKING_ON_OBJ, to.getObjectId()))
                Try.ofFailable({
                    logger.logDebug("Trying Selenium click !");
                    webElement.click();
                    return Boolean.TRUE;
                }).orElseTry({
                    logger.logDebug("Trying to scroll to the element, wait for it to be clickable and use Selenium click !");
                    scrollToElement(webDriver, webElement);
                    WebDriverWait wait = new WebDriverWait(webDriver, timeout);
                    webElement = wait.until(ExpectedConditions.elementToBeClickable(webElement));
                    webElement.click();
                    return Boolean.TRUE;
                }).orElseTry({
                    logger.logDebug("Trying Javascript click !");
                    JavascriptExecutor executor = (JavascriptExecutor) webDriver;
                    executor.executeScript("arguments[0].click();", webElement);
                    return Boolean.TRUE;
                }).onSuccess({
                    logger.logPassed(MessageFormat.format(StringConstants.KW_LOG_PASSED_OBJ_CLICKED, to.getObjectId()))
                }).get();
            } finally {
                if (isSwitchIntoFrame) {
                    WebUiCommonHelper.switchToDefaultContent()
                }
            }
        }, flowControl, true, (to != null) ? MessageFormat.format(StringConstants.KW_MSG_CANNOT_CLICK_ON_OBJ_X, to.getObjectId())
        : StringConstants.KW_MSG_CANNOT_CLICK_ON_OBJ)
    }
}

Please read the code to find how the click method is implemented.

The click method sequentially tries the following 3 steps:

  1. try to click the HTML element by the ordinary WebElement.click() method. If failed, will fall down to the following step.
  2. try to scroll to the element and click the element by WebElement.click() with Wait. The Wait will expire after 30 seconds as default. If failed, will fall down to the following step.
  3. try to click the element by executing a javascript arguments[0].click()

The implementation is as such that the Enhanced Click is likely to take 30 seconds or longer at the step2.

@grylion54 suggested to go to the step3 immediately skipping the step2. It will run quickly.

The Enhanced Click keyword involves a JavaScript call, which is not really “additional”, rather is essencial.

1 Like

Hi @kazurayam and @grylion54 ,

Thank you both for your messages and assistance.

@kazurayam , do you mean to extract step 2 from the code? I am a bit confused about how I can integrate the Enhanced Click feature with the code that @grylion54 suggested. Could you please provide further guidance on this?

All I am saying is, you don’t need the Enhanced Click at all.

Yes I am using the Web Recorder.

So, the solution for Web Recorder is to create a new keyword named, for example, clickJS and insert the code you mentioned above? Can you please explain more? Sorry for the questions, I am a beginner in Katalon…

I don’t understand your question.

Can you please explain the steps I must follow to resolve the issue with the web recorder? I am getting confused.

I have created the custom keyword ‘clickKeywordJS’ and now I want to replace it with the default click keyword so that I don’t have to manually change it every time. Is it possible?
image

So to give me you more details, if I will use the web recorder then automatically will inherit the ‘clickKeywordJS’ instead of click.

1 Like

Yes, it is possible.

You want to use Groovy’s feature called “Expando meta class”. Read the doc:

https://www.groovy-lang.org/metaprogramming.html#metaprogramming_emc

I have applied this technique to my Katalon projects several times. See the following post for example:

Be informed, in order to utlilize the “Metaprogramming” techinique, you need to read the source code of katalon studio WebUI API. I decribed how to get the source codes in the previous post

1 Like