How to use Javascript Executer inside loop to click on object?

I have a situation where I am adding a new record to a data table. After that i delete the same record from my data table. So, to delete it, I have to loop through the entire table to find that row. I managed to do it and everything went fine until i ran the test with Task Scheduler. I got the following error:

org.openqa.selenium.WebDriverException: unknown error: Element … is not clickable at point (951, 512). Other element would receive the click:

In the past i have dealt with this error using this Javascript click way. That is:

WebElement element = WebUiCommonHelper.findWebElement(findTestObject(testObject),30)

WebUI.executeJavaScript(“arguments[0].click()”, Arrays.asList(element))

Now I am using a loop and don’t really have an idea how to use this method properly inside it or if there even is a way to use it. Below is the code I am using:

WebDriver driver = DriverFactory.getWebDriver()

String denomination = GlobalVariable.Denomination

    'To locate table'
WebElement Table = driver.findElement(By.xpath("//div[@id='ucform:items-dt']/div/table"))

List<WebElement> rows_table = Table.findElements(By.tagName('tr'))

//To calculate no of rows In table
int rows_count = rows_table.size()

Loop:
for (int row = 0; row < rows_count; row++) {
    
    //To locate columns(cells) of that specific row
    List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'))
     
    //To calculate no of columns(cells) In that specific row
    int columns_count = Columns_row.size()
    
    for (int column = 0; column < columns_count; column++) {
        
        if(Columns_row.get(column).getText().equalsIgnoreCase(denomination)){
            
            Columns_row.get(7).click()
            
            break Loop;
        }
    }
}

Basically the code above finds all the rows from the table and finds the one that contains my added record. Then i tell it to click on the column with index 7 (the button i want to click is in that column). It works fine in browser mode where I can see the test running, but doesn’t work when the test runs in the background and I can’t see its flow. Any help resolving this problem would be very much appreciated :slight_smile:

Firstly, are you running headless?

This kind of error can mean that the click action is likely to hit something other than your intended target.

Try maximizing the window.
Try waiting until the element is visible.
Try scrolling the element into view.
Wait a little longer still.

If you are running headless, change it to run non-headless and set up a scheduled task to run immediately and watch it. Let us know the difference.

A word on the code:

Do you need the inner loop? Is the test for denomination vital? If the cell is always cellIndex=7 then you can shorten the code considerably.

In Task Scheduler, I am running headless (I cannot/don’t know how to change it, tried adding: -browsertype=“Chrome” to my script, but it doesn’t change anything) and it fails. In the Katalon app I am running with non-headless (I can see my tests running in browser) and my test passes.

Maximizing the window results in the test not starting at all in task scheduler, it can’t get pass the WebUI.maximizeWindow() command (though works fine in non-headless).

Adding delays won’t help also.

I don’t know how to use waitForElementVisible or scrollToElement as I am not sure what my testObject is. Columns_row.get(7) is what I click on, but how to I use it as the object. For example: WebUI.waitForElementVisible(findTestObject(Columns_row.get(7))). I know this doesn’t work as findTestObject must be a string etc, but how to I use it then? :smiley:

You can convert the Web Elements to Test Objects:

Did you try adding WebUI.waitForElementClickable() before Columns_row.get(7) ?

After you’ve converted the Web Element to Test Object, of course…

To be honest, I don’t really understand the instructions given in the topic you advised.

public TestObject createTestObject(String locator){

TestObject updatedTestObject = new TestObject("Grid")  

updatedTestObject.addProperty("xpath", ConditionType.EQUALS, locator)  

return updatedTestObject  

}

This is marked as the solution, but I do not understand what is meant by locator. Is it the xpath of the button I want to click or what?

Yes. You’re creating a shiny new TestObject (just like the Test Objects you have stored in your Object Repository) but “live in memory”. When Katalon tries to use your TO, it needs to know how to find it on the screen, that’s what the xpath does - it locates the web element by using the TO’s properties, one of which is its xpath.

Does that make sense?

Yeah, it makes sense, but using the method above brought up another question. I managed to click on the desired object, but index of the added row might not always be the same and the xpath that I use as parameter for locator has a fixed index. So my question is, how to get a dynamic index for my Columns_row.get(7)?

int my_new_index = something
...
Columns_row.get( my_new_index )

Thank you for the help, I managed to solve my problem and now everything works fine when running from Task Scheduler.