ritah
ritah

Reputation: 1

how to handle Stale Element Reference Exception while finding element in JMeter

i am trying to find element whose structure something like below

<div>
 <div>
  <div>
    <ul>
     <object id = "obj"
       #document (here is a link )
        <html>
         <head> </head>
          <body>
            <div id = style4 >

i have tried multiple ways to find this element but it always gives exception of stale element here's what i've done

 var table = wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.xpath('/html/body/div[8]/div/div/div[2]/div/div[2]/div/div/ul')));
    WDS.browser.executeScript("arguments[0].scrollIntoView(true);", table);
    WDS.log.info("Got Table");

    // Find the <object> element within the table
    var objectElement = table.findElement(pkg.By.tagName('object'));
    WDS.log.info("Found objectElement");

    // Extract the content of the <object> element
    var objectContent = objectElement.getAttribute('data');
    WDS.log.info("Extracted objectContent");

    // Refresh the <object> element to prevent staleness
    WDS.browser.executeScript("arguments[0].scrollIntoView(true);", objectElement);
    WDS.log.info("Refreshed objectElement");

    // Load the content document of the <object> element
    var contentDocument = WDS.browser.executeScript("return arguments[0].contentDocument", objectElement);
    WDS.log.info("Loaded contentDocument");

    // Find the <html> element within the content document
    var htmlElement = contentDocument.querySelector('html');
    WDS.log.info("Found htmlElement: " + htmlElement);

and this is the log

2024-02-21 22:42:12,278 INFO c.g.j.p.w.s.WebDriverSampler: Got Table
2024-02-21 22:42:12,289 INFO c.g.j.p.w.s.WebDriverSampler: Found objectElement
2024-02-21 22:42:12,304 INFO c.g.j.p.w.s.WebDriverSampler: Extracted objectContent
2024-02-21 22:42:12,309 INFO c.g.j.p.w.s.WebDriverSampler: Refreshed objectElement
2024-02-21 22:42:12,317 ERROR c.g.j.p.w.s.WebDriverSampler: Error: org.openqa.selenium.StaleElementReferenceException: stale element reference: stale element not found
  (Session info: chrome=122.0.6261.58)

Upvotes: 0

Views: 59

Answers (1)

Ivan G
Ivan G

Reputation: 2707

You've create the problem yourself by using the JavascriptExecutor. Do you think that real user using the real browser will really open the browser developer tools, switch to console and type var table = document.getElementById....)? I strongly doubt.

In any case you can protect yourself from StaleElementReference errors by using Explicit Wait for any element operation like instead of this:

var element = WDS.browser.findElement(pkg.By.id('foo'))
//something else
element.click()

you can do:

wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.id('foo'))).click()

Also regarding using JavaScript - it might be not the best idea, in general it's recommended to use JSR223 Test Elements and Groovy language for scripting in general because Groovy provides the best performance. Moreover Nashorn engine has been removed in JDK 15 hence you won't be able to use JavaScript language in later Java versions.

Upvotes: 0

Related Questions