Selenium Checkbox StaleElementReferenceException

I have a function that sets a checkbox.  It involves a couple of steps:

  1. var cb = FindElement(locator)
  2. if (cb.Selected != newValue)
  3. cb.Click();
  4. verify cb.Selected equals newValue.

I find that if I do all 4 steps within one method, they work fine. 

Problem

I tried to code refactor.  So I created method A with step1 in it and it calls method B( step 2 & 3 & 4).  However, I start getting StaleElementException at Step 4.

Solution

1. A lot of people use try/catch to retry and it works.

2. In my specific, I find that if I have step 1 & 2 within one method, it is OK.

Speculation

I don’t know the cause of the problem.  I guess this has to do with web page using modal dialog.  Click() seems to screw up the element.  It seems like one must call .Selected() right after it FindElement().

 

References:

http://stackoverflow.com/questions/4846454/selenium-webdriver-staleelementreferenceexception

http://stackoverflow.com/questions/31379812/selenium-staleelementreferenceexception-explicit-wait

http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s