0

*Iam getting a stale element Exception in this part:

I am getting the id from the table

this is the part where it leads to stale element:


latestId.click();
        
    

I have tried: using the below code:

 WebDriverWait wait = new WebDriverWait(driver,10);```
  wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf(latestId)));

Basically in the method since both the id.click() and createdvalue("id",latestid.gettexxt()) is there it is causing the problem.

Can i get a solution for this ..Can Add more information if required*

sandy
  • 27
  • 6
  • what is `tableValueSize ` and how have you declare it ? – cruisepandey Aug 24 '21 at 13:28
  • WebElement latestId = driver.findElement(By.xpath("(//table//tr//td[contains(@class,'mat-column-demandId')]//span)["+tableValueSize +"]")); and the size is 7 ...Any leads – sandy Aug 24 '21 at 13:29
  • See in general stale element is because of navigation meaning, you navigated to some page and when you are back on the page from where you navigated, elements becomes stale. so for solving this, we have to define web elements again when we are back. Now I need to know what are your steps in your automation test ? – cruisepandey Aug 24 '21 at 13:43
  • 1)i naviagte the the plan screen where i fetch the latest id by using the fetchid method – sandy Aug 24 '21 at 14:37
  • @cruisepandey:My Steps: 1)First i go to plan screen and get the lastestid from the table 2)Next i click on the id and click on send proposal button 3)this is the place where iam now getting stale element - once i click on sened proposal in the previous screen the proposal will then come to the pending confirm table.4)So then i read the table and take the latest id and append the id with the url.... the point is iam getting the stale element while getting the id ..so any leads on this... – sandy Aug 24 '21 at 14:57

1 Answers1

0

You haven't share the element's ID finding code.

Here is was I advise you to do:

  1. Find the element again by its known ID.
  2. Wait for the element to be clickable
  3. click on it

Code:

    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement myElement = wait.until(ExpectedConditions.elementToBeClickable(By.id("TheID")));
    try {
        Actions builder = new Actions(browser);
        builder.moveToElement(myElement).click().build().perform();
    } catch (Exception x1) {
        myElement.click();
    }
Tal Angel
  • 1,301
  • 3
  • 29
  • 63
  • Actually the issue is in this part :createdValue.put("latestDataId", latestId.getText()); System.out.println(createdValue.put("latestDataId", latestId.getText())); that is bz iam using the same method to click on the id - latestId.click(); and then i use createdValue.put("latestDataId", latestId.getText()); to get the text and append the text to the url .... this is the issue as a result of which i get stale element any leads on this.I was trying with different things and this is the issue.So when i try to get the id and append it to the url it is causing a stale element exception – sandy Aug 25 '21 at 02:33
  • @ cruisepandey ,@Tal so i need to have both the latestid.click and cretedvalue.put the latestid in the same method since first i need to click on the id in one screen and then append the id with the url in another screen – sandy Aug 25 '21 at 03:18
  • @Tak Angel - is there something that i need to add to ensure that i dont see this issue – sandy Aug 25 '21 at 09:07
  • @sandy Lol, my name is Tal not Tak. If you do this in a loop - create a new Wait object in the loop and use it all the time. Also, before clicking try to wait 1-2 seconds. – Tal Angel Aug 25 '21 at 09:12
  • actually the issue is since i have the click and createdValue.put("latestDataId", latestId.getText()); in the same method it is also clicking on the id when i dont want that to happen and want to only fetch the id and append to the url so how should i do it in a loop?have looped you to another post where i have given a clear explanation on this hope that is ok@Tal Angel – sandy Aug 25 '21 at 09:20
  • Any inputs for this issue which is clearly said in other post – sandy Aug 25 '21 at 09:42