Re: [selenium-users] Refresh Selenium WebDriver DOM tree - correcting a lot of NoSuchElement, StaleReference exceptions

8,875 views
Skip to first unread message

Mark Collin

unread,
May 28, 2013, 4:01:21 AM5/28/13
to seleniu...@googlegroups.com
I hate to say it but your problems are indicative of badly constructed
test cases and/or lack of understanding of how the website you are
automating works.

NoSuchElementException's are caused because when selenium looked at the
page the element wasn't there, remember that Selenium is fast, much
faster than a human so it is quite able to check the DOM *before* all
JavaScript rendering has taken place. The solution to these issues is
to use an explicit wait and wait for the element to appear.

If you don't want to use explicit waits and you want a bit of a hacky
solution that will work in most cases try using an implicit wait,
whatever you do don't mix implicit and explicit waits, things will start
going wrong, explicit waits are the recommended solution:

http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits

StaleElementReferenceException's are caused by the DOM being refreshed
after you found an element. Remember that a WebElement is a reference
to a specific element on the page, if the DOM get's refresed this
reference is broken and you need to find the element again to be able to
interact with it. The best way to deal with these in my opinion is to
start using page factories. The @FindBy annotation will ensure that the
element if found every time you use it so you will not suffer from
StaleElementReferenceException's (Well OK there is a slim possibility
that it can occure if the DOM changes in the couple of ms between it
finding the element and doing something with it, but that's a real edge
case.

https://code.google.com/p/selenium/wiki/PageFactory


On 27/05/2013 10:24, Peter Merkac wrote:
> Hi Selenium friends,
>
> With my coworkers we are currently working on a project for automating
> GUI tests execution for insurance web applications. Lately we had a
> lot of instability problems with our Selenium test automation kit
> (a.k.a. SeTAK), which is built on top of Selenium WebDriver. Those
> Selenium errors were related to following three areas:
> - switching of frames and consequently (NoSuchElementException)
> - generally elements were not found (when searching by id, name or
> xPath) although they were visible on the web-page
> - stale reference exceptions also occurred again with elements visible
> on the web-page
>
> Than we came across a solution to retry to execute particular selenium
> commands like in the below link
>
> http://stackoverflow.com/questions/4846454/selenium-webdriver-staleelementreferenceexception
>
> While some of the above mentioned errors were corrected as suggested
> by the retry solution, a situation, that particular DOM element was
> NOT present in the Selenium WebElement tree, although VISIBLE on the
> web-page of target web-application, has NOT been tackled. From our
> experience such situations were undeterministic, occured randomly and
> more often on the DOM elements, which had some JavaScript events e.g
> "onchange", ... in-behind.
> We came to the conclusion that there MUST be some bug in refreshing
> the tree of WebElements as maintained by the selenium WebDriver, which
> points to actual DOM three of a target web-page. Having this in mind
> we implemented a simple but VERY effective solution (tested on our
> test environment multiple times), which has been driven by the below
> Stackoverflow force DOM refresh without page reload. Our requirement
> is, that with DOM refresh no page reload is done.
>
> http://stackoverflow.com/questions/8840580/force-dom-redraw-refresh-on-chrome-mac
>
> From our experience with above described errors we suggest instead of
> retrying to interpret selenium commands (or additionally to retry
> solution) to REFRESH DOM tree of a web-page with javascript as
> suggested in the above link and than retry to execute web driver commands.
>
> @Override
> public void refreshDOM(String elementId) throws WebDriverException {
> switchFrame(getActiveFrameId(), 5);
> JavascriptExecutor jsExecutor = (JavascriptExecutor) getWebDriver();
> jsExecutor.executeScript(buildJQueryScriptDerictive());
> jsExecutor.executeScript("$('#" + elementId + "').hide().show();");
> }
>
> Of course such DOM refresh has to be used with care in sense of
> time-execution impact only when a particular undetermined exception
> occurs like in the case below.
>
> public IWebTestResult
> interpretPredicateWithRetry(IInterpretationContext interpretationContext)
> throws WebDriverException {
> try {
> interpretAndLogPredicate(interpretationContext,
> logMessage, false, 1);
> }
> catch (NoSuchElementException e1) {
> interpretationContext.getPredicateHandler().refreshDOM("UI-MAIN_PANEL");
> // Retry to interpret commands
>
> }
> catch (StaleElementReferenceException e2) {
> interpretationContext.getPredicateHandler().refreshDOM("UI-MAIN_PANEL");
> // Retry to interpret commands
> }
> catch (NullPointerException e3) {
> interpretationContext.getPredicateHandler().refreshDOM("UI-MAIN_PANEL");
> // Retry to interpret commands
> }
> catch (TimeoutException e4) {
> interpretationContext.getPredicateHandler().refreshDOM("UI-MAIN_PANEL");
> // Retry to interpret commands
> }
> finally {
> interpretationContext.setWaitInterpretationHandler(new
> DefaultWaitInterpretationHandler());
> }
> return predicateResult;
> }
> We are passing in an id of top-parent element, but to fine tune this,
> an exact parent element can be found via java-script to decrease
> performance impact.
>
> This solution brought a LOT of stability to our selenium gui tests,
> therefore we wanted to share it with all of you.
>
> With best regards,
> Peter, Toni
> --
> You received this message because you are subscribed to the Google
> Groups "Selenium Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to selenium-user...@googlegroups.com.
> To post to this group, send email to seleniu...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/selenium-users/236566e3-8d82-47ba-ab7a-6e7d5b7af3d1%40googlegroups.com?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Muralisankar A

unread,
Nov 4, 2014, 8:06:14 AM11/4/14
to seleniu...@googlegroups.com
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class Testcases {
   
    WebDriver driver;
    String baseurl="http://192.168.0.190/users/login/";
   
    @BeforeTest
    public void setup() {
       
        driver=new FirefoxDriver();
        driver.get(baseurl);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
     @Test
      public void TC8() {
         
          driver.findElement(By.id("id_username")).sendKeys("admin");
          driver.findElement(By.id("id_password")).sendKeys("admin");
          driver.findElement(By.cssSelector("button,input[type='button']")).click();
          Reporter.log("TC101 > Login successfully to crossfraudet");
      }
     
     @Test
     public void TC9() {
       
          driver.findElement(By.cssSelector(".icon-users"));
             
     }
      @Test
      public void TC10() {
       
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          driver.findElement(By.cssSelector(".icon-users")).click();
          driver.findElement(By.cssSelector("#menuList > li.dropdown.open > ul > li:nth-child(3) > a")).click();
          Reporter.log("TC102 > Click on User Profile > Profiles");  
         
      }
     
   
}

[TestNG] Running:
  C:\Users\Murali\AppData\Local\Temp\testng-eclipse-769164162\testng-customsuite.xml

PASSED: TC8
PASSED: TC9
FAILED: TC10
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".icon-users"}
Command duration or timeout: 10.04 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html



How can I solve this issue? Anybody please help me

PeterJeffreyGale

unread,
Nov 4, 2014, 8:11:28 AM11/4/14
to seleniu...@googlegroups.com

On Tuesday, 4 November 2014 13:06:14 UTC, Muralisankar A wrote:

How can I solve this issue? Anybody please help me

1. Don't hijack threads
2. Don't use findElement() until just before you are about to use the elements you are seeking. 

Muralisankar A

unread,
Nov 4, 2014, 8:23:39 AM11/4/14
to seleniu...@googlegroups.com

Hi peter, thanks for your reply.

Please send me a sample code to solve this issue


--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/3cshBBojFrE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

PeterJeffreyGale

unread,
Nov 4, 2014, 8:23:49 AM11/4/14
to seleniu...@googlegroups.com
You can write you're own code. Just apply the principle suggested.

Muralisankar A

unread,
Nov 4, 2014, 8:26:39 AM11/4/14
to seleniu...@googlegroups.com
I have been struggling to find the element on the page after logged in to my application.

Muralisankar A

unread,
Nov 4, 2014, 8:31:38 AM11/4/14
to seleniu...@googlegroups.com
It shows the same error if I use the xpath driver.findElement(By.xpath("/html/body/div/div/section/nav/ul/li[3]/a/i")) instead of css selector driver.findElement(By.cssSelector(".icon-users"))

Muralisankar A

unread,
Nov 4, 2014, 11:04:53 AM11/4/14
to seleniu...@googlegroups.com

@Test
       public void TC9() {                       
         WebDriverWait wait = new WebDriverWait(driver, 30);
           wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".icon-users")));
         }

      @Test
      public void TC10() {       
        WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".icon-users")));

        driver.findElement(By.cssSelector(".icon-users")).click();
        driver.findElement(By.cssSelector("#menuList > li.dropdown.open > ul > li:nth-child(3) > a")).click();
        Reporter.log("TC102 > Click on User Profile > Profiles");  
      }


PASSED: TC8
PASSED: TC9
FAILED: TC10
org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for visibility of element located by By.selector: .icon-users
Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:33'
System info: host: 'BDMAGFPC009', ip: '192.168.12.118', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_05'
Driver info: org.openqa.selenium.firefox.FirefoxDriver

Reply all
Reply to author
Forward
0 new messages