I tried to delete an added category from a list of categories. First category deleted successfully. But it did not click the next delete icon

I tried to delete an added category from a list of categories. First category deleted successfully. But it did not click the next delete icon. org.openqa.selenium.StaleElementReferenceException: stale element reference: stale element not found Exception occured

See the below code

TestObject testObj = findTestObject(‘CategoryEdit/MultiCatDelete’)
List elements1 = WebUI.findWebElements(testObj, 10)
int a = elements1.size()
System.out.println(‘Size is…’ + a)

for(WebElement ListValues : elements1) {
	ListValues.click()
	
    WebUI.waitForElementVisible(findTestObject('CategoryEdit/Page_IBOGO/button_Yes, delete it'), 0)

    WebUI.click(findTestObject('CategoryEdit/Page_IBOGO/button_Yes, delete it'))

    WebUI.verifyElementVisible(findTestObject('CategoryEdit/Page_IBOGO/div_Course menu deleted successfully'))
	WebUI.delay(10)
}

Please suggest soulutions

1 Like

The likely issue is that once you delete the first item, all the other items on the page get refreshed to be in their “new” position. You will generally not be able to just run through the list of items and delete them. You may have to reread the page to get your next item in the list, like:


List<WebElement> elements1 = WebUI.findWebElements(findTestObject('CategoryEdit/MultiCatDelete'), 10)
int a = elements1.size()

System.out.println("Size is…${a}")

int cnt = 0

while (a > 0) {
	elements1.get(cnt).click();
    WebUI.waitForPageLoad(10)

    WebUI.waitForElementVisible(findTestObject('CategoryEdit/Page_IBOGO/button_Yes, delete it'), 10)

    WebUI.click(findTestObject('CategoryEdit/Page_IBOGO/button_Yes, delete it'))

    WebUI.verifyElementVisible(findTestObject('CategoryEdit/Page_IBOGO/div_Course menu deleted successfully'))
	WebUI.delay(10)  // maybe try WebUI.waitForPageLoad(10)

    elements1 = WebUI.findWebElements(findTestObject('CategoryEdit/MultiCatDelete'), 10)
    a = elements1.size()
}

Another idea:
I have one test that I have to create multiple accounts and then close them out. The issue is that when the account is closed the list of accounts changes as the account status goes from open to close. What I have to do in this case after I have created all the accounts is to start at the last account and then I can close them out one after the other–without rereading the list. In this case the change to the list is behind where I am working and does not affect the accounts that are still “open”. So, maybe you can try to start at the last item to delete instead of the first and see if you can simply loop through them.

And finally:
The other item I changed in the code above is your “waitFor” statement. There is no value in having the statement if you are only going to wait for 0 seconds. Even if the wait is only 0.5 seconds, that is too long for you and it errors out. Either delete the statement or change it to how I use it, like 5 or 10 seconds. Personally, change it to as above.

2 Likes

You also have your breadcrumb in the heading as Katalon Recorder. You may be using the Web Recorder of Katalon Studio. Katalon Recorder is another application entirely.

1 Like

it worked…!

1 Like