Stale element reference: element is not attached to the page document

Hi Team,
Please give the solution for the below error.
Functionality:-
I have a web table with 5 columns and some rows .in the rows there are some files ,
Here i need to check, if the second column(I.e document type) has the text with tax then i need to click on 1st column file(i.e a link).
But here i need to click on a pagination button.
For the fist page script is working but after clicking on the pagination button script is trying to get the rows from the next page(Table x path is same for all the pages)there i am getting the error that ‘stale element reference: element is not attached to the page document’
Code:-
for (int s = 1; s <= (r - 1); s++) {
String Disable_Value=button_Page.get(1).getAttribute(‘disabled’)
println(Disable_Value)
if(Disable_Value != true){
for (int i = 0; i <= (all_Types.size() - 1); i++) {
List elements = drivernewchange.findElements(By.xpath(‘//[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody’))
for (int l = 1; l <= elements.size(); l++) {
WebElement element = drivernewchange.findElement(By.xpath(('//
[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody’)))
}
List all_Types_td = all_Types.get(i).findElements(By.tagName(‘td’))
println(all_Types_td.size())
List elements1 = drivernewchange.findElements(By.xpath(‘//[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody’))
for (int l = 1; l <= elements1.size(); l++) {
WebElement element1 = drivernewchange.findElement(By.xpath(('//
[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody’)))
}
String type_Text = all_Types_td.get(1).getText()
println(type_Text)
if (type_Text == ‘Tax’) {
println(‘swathi’)
List a_tags = all_Types_td.get(0).findElements(By.tagName(‘a’))
println(a_tags.size())
String test = a_tags.get(0).getText()
println(test)
list.add(counter, test)
WebUI.delay(5)
actions1.click(a_tags.get(0)).perform()
WebUI.delay(15)
counter = counter++
}
}
actions1.click(button_Page.get(1)).perform();
WebUI.delay(30)
println(list)

			}

Error Screenshot:-


Application Screenshot:-

Please help me to solve this.

Thanks,
Swathi

Your indenting is horrible, so hard to see. Same goes for the code above it.
Stale references to web elements appear when navigating away from the page. You need to refetch them.

Hi r.klein1,

Thank you for the quick response .
You have any idea how we need to refetch??

Thanks,
Swathi

Your code is not complete, so I can’t check it to be sure, but you are doing somewhere: “actions1.click(a_tags.get(0)).perform()”. I guess that’s where you navigate to somewhere? Or maybe a couple of lines below that where you do another click on ‘actions1’ (whatever that is).

Also … what are you trying to do with code like this:

for (int l = 1; l <= elements.size(); l++) {
WebElement element = drivernewchange.findElement(By.xpath((’//
[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody’)))
}

Yes your correct “actions1.click(a_tags.get(0)).perform()”. by using actions method i am clicking on pagination button.

I got the below code from forum(Some one posted same error they are given the below solution.so I used this but it is also not working)
for (int l = 1; l <= elements.size(); l++) {
WebElement element = drivernewchange.findElement(By.xpath((’//
[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody’)))
}

Here is the full script:-
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import org.openqa.selenium.interactions.Actions as Actions

import com.kazurayam.webdriverfactory4ks.ChromeDriverFactory as ChromeDriverFactory
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.closeBrowser()

//****************************************************************************
WebUI.closeBrowser()

//CRM login*****************88
ChromeDriverFactory cdFactory = new ChromeDriverFactory()

WebDriver driver = cdFactory.openChromeDriverWithProfile(‘Katalon’ // THIS IS THE MAGIC!
)

assert driver != null

DriverFactory.changeWebDriver(driver)

WebUI.navigateToUrl(‘https://test.salesforce.com’)

WebUI.maximizeWindow()

WebUI.delay(5)

WebUI.click(findTestObject(‘Object Repository/Old_Sprints/Page_Login Salesforce/input_Username_username’))

WebUI.setText(findTestObject(‘Object Repository/Old_Sprints/Page_Login Salesforce/input_Username_username’), ‘test’)

WebUI.setText(findTestObject(‘Object Repository/Old_Sprints/Page_Login Salesforce/input_Password_pw’), ‘test’)

WebUI.delay(5)

WebUI.click(findTestObject(‘Object Repository/Old_Sprints/Page_Login Salesforce/input_Password_Login’))

WebUI.delay(20)

WebUI.click(findTestObject(‘Old_Sprints/slect option from the list_CRM(eg,contact,account)’))

WebUI.delay(5)

WebDriver drivernewchange = com.kms.katalon.core.webui.driver.DriverFactory.getWebDriver()

String ExpectedValue1 = ‘Account’

WebElement Tableulchange = drivernewchange.findElement(By.xpath(’/html/body/div[4]/div[1]/section/header/div[2]/div[2]/div/div[1]/lightning-grouped-combobox/div/div/lightning-base-combobox/div/div[2]/ul[1]’))

//TestObject tableTestObject = findTestObject(‘Object Repository/Old_Sprints/WebTable_Mawer’)
//CustomKeywords.‘com.reusableComponents.HighlightElement.run’(tableTestObject)
‘To locate rows of table it will Capture all the rows available in the table’
List rows_tablechange = Tableulchange.findElements(By.tagName(‘li’))

‘To calculate no of rows In table’
int rows_countchange = rows_tablechange.size()

println('Cell Value Of row number ’ + rows_countchange)

rows_tablechange.get(2).click()

WebUI.setText(findTestObject(‘Old_Sprints/Serchbox_CRM’), ‘John Client’)

WebUI.delay(20)

//@@@@@@@@@@@@@@@@@@@@@@@@@click on john CLient account@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WebElement Tablenewchange = drivernewchange.findElement(By.xpath(’//*[@id=“oneHeader”]/div[2]/div[2]/div/div[2]/div/div[2]/div[2]/div/div/div[2]/ul’))

List rows_tablenewchange = Tablenewchange.findElements(By.tagName(‘li’))

int rows_countnewchane = rows_tablenewchange.size()

println('Cell Value Of row number ’ + rows_countnewchane)

rows_tablenewchange.get(1).click()

WebUI.delay(15)

//@@@@@@@@@@@@@@@@@@@@@@@@@@@Click on More@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WebUI.click(findTestObject(‘Old_Sprints/More’))

WebUI.delay(10)

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WebUI.click(findTestObject(‘Old_Sprints/More_Documents_UAT’))

//WebUI.click(findTestObject(‘Old_Sprints/More_Documnets’))
WebUI.delay(20)

//WebUI.verifyElementPresent(findTestObject(‘Old_Sprints/Page Nation’), 10)
//WebUI.delay(5)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Table body@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WebElement Document_Type = drivernewchange.findElement(By.xpath(’//*[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody’))

List all_th = Document_Type.findElements(By.tagName(‘tr’))

List all_Types = Document_Type.findElements(By.tagName(‘tr’))

println(all_Types.size())
String val1=all_Types.size()
Actions actions1 = new Actions(driver)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@page nation@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
def list = []
int counter = 0
if(val1==‘10’){
WebElement page_nation = drivernewchange.findElement(By.xpath(’//*[@id=“tab-10”]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/div’))

	List<WebElement> button_Page = page_nation.findElements(By.tagName('button'))
	println(button_Page.size())
	String title_Value=button_Page.get(1).getAttribute('title')
	println(title_Value)
	//String Disable_Value=button_Page.get(r).getAttribute('disabled')
	if(title_Value=='forward'){
		List<WebElement> label_Page = page_nation.findElements(By.tagName('label'))
		println(label_Page.size())
		String finaltext=label_Page.get(0).getText()
		println(finaltext)
		String [] a = finaltext.split(' ')
		println(a[1])
		println(a[0])
		int r = Integer.parseInt(a[1])
		for (int s = 1; s <= (r - 1); s++) {			
			String Disable_Value=button_Page.get(1).getAttribute('disabled')
			println(Disable_Value)
			if(Disable_Value != true){			
				for (int i = 0; i <= (all_Types.size() - 1); i++) {	
					List<WebElement> elements = drivernewchange.findElements(By.xpath('//*[@id="tab-10"]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody'))				 
						 for (int l = 1; l <= elements.size(); l++) {
							 WebElement element = drivernewchange.findElement(By.xpath(('//*[@id="tab-10"]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody')))
						 }
				   List<WebElement> all_Types_td = all_Types.get(i).findElements(By.tagName('td'))
					println(all_Types_td.size())
					List<WebElement> elements1 = drivernewchange.findElements(By.xpath('//*[@id="tab-10"]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody'))
					 for (int l = 1; l <= elements1.size(); l++) {
						 WebElement element1 = drivernewchange.findElement(By.xpath(('//*[@id="tab-10"]/slot/flexipage-component2/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div[2]/table/tbody')))
					 }
					String type_Text = all_Types_td.get(1).getText()
					println(type_Text)
					if (type_Text == 'Tax') {
						println('swathi')
						List<WebElement> a_tags = all_Types_td.get(0).findElements(By.tagName('a'))
						println(a_tags.size())
						String test = a_tags.get(0).getText()
						println(test)
						list.add(counter, test)
						WebUI.delay(5)
						actions1.click(a_tags.get(0)).perform()				
						WebUI.delay(15)
						counter = counter++
					}
				}
				actions1.click(button_Page.get(1)).perform();
				WebUI.delay(30)
				println(list)
				
			}
			
		}
		
		
	}

}

I’ll bet the problem is that “all_Types” isn’t available anymore after the click and gives the stale reference.

Also the code you got from the forum as it is now is useless. You are fetching an object INSIDE the for loop, which means it will not be available outside the for loop.

Here are screenshots to give more clarity on ‘all_Types’, that are available after clicking. please refer below.

before clicking the pagination:-


after clicking on pagination:-

after clicking on the pagination to next then i am getting the stale element error

Yes, so AFTER clicking, you need to refetch them

what is the way to re-fetch after clicking on next in the page.

if you mean refetch is refreshing the page here i have a challenge in refreshing the page. if i refresh the page it will be redirecting to the home page.

Thanks.

So to fix the exception, you could do:

all_Types = Document_Type.findElements(By.tagName(‘tr’))

after you’ve clicked… but that’s not really logical. What you are then doing is re-fetching the pagination links after you’ve clicked on it, but continuing based on the first list you’ve fetched, example

I have pagination “1,2,3,4, …” --> this would give a list with links [1,2,3,4]. Now you click for example on the 2nd item (=2). Next list on the page COULD be “1,2,3,4,…” again. In that case it WILL go correctly since on page 2 the items are the same. But if the items aren’t the same on page 2, you could get something like “… 2, 3, 4, 5, …”. That would mean you’ll get a list like [2,3,4,5].
The next run (for loop) you’ll be clicking on the next item (3rd item, thus 4).

You should rethink the test case how you would do it manually… for example: How many pages do I have (in your case it says ‘4’). So you loop from 1 to 4 and click on each ‘next’ button. Keep the current page number in a variable and check if that’s updated every time until you’ve reached 4. Then you shouldn’t be able to click anymore.

1 Like

This is working thanks for your help :slight_smile: Much appreciated for your time.

Thanks.