0

I have an issue with a selenium web test I'm executing. I keep getting a stale element reference error after clicking and taking me to the next page(which is correct)


WebDriver driver = DriverFactory.getWebDriver()

WebElement PositionTable = driver.findElement(By.xpath('//td[2]/table/tbody/tr/td/table'))

List<WebElement> Rows = PositionTable.findElements(By.tagName('tr'))

println('No. of rows: ' + Rows.size())


table: for (int i = 0; i < Rows.size(); i++) {
    
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    
   for (int j = 0; j < Cols.size(); j++) {
       if (Cols.get(j).getText().equalsIgnoreCase(ExpectedPosition)) {
           
           
          Cols.get(j).findElement(By.tagName('a')).click()
          WebUI.delay(5)

          table: break
          
          } 
          }
            
          }

1 Answers1

0

If clicking a button changes anything in the page html , the reference gets lost. You have find all elements again. So change your code as below

WebElement PositionTable = driver.findElement(By.xpath('//td[2]/table/tbody/tr/td/table'))

List<WebElement> Rows = PositionTable.findElements(By.tagName('tr'))

println('No. of rows: ' + Rows.size())

Int countrow = 0 


table:     while(countrow<Rows.size) {
++countrow
PositionTable = driver.findElement(By.xpath('//td[2]/table/tbody/tr/td/table'))

Rows = PositionTable.findElements(By.tagName('tr'))
    
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    Int countcol = 0 
    
   whil(countcol < Cols.size()) {
       ++countcol
       Cols = Rows.get(i).findElements(By.tagName('td'))
       if (Cols.get(j).getText().equalsIgnoreCase(ExpectedPosition)) {
           
           
          Cols.get(j).findElement(By.tagName('a')).click()
          WebUI.delay(5)

          table: break
          
          } 
          }
            
          }
PDHide
  • 18,113
  • 2
  • 31
  • 46