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. 


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.


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.


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().




Leave a Reply

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

You are commenting using your 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