title: Selenium c# - OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
body: <p>Hopefully someone can help on this issue...</p>
  
  <p>I keep receiving the above error message (see title) when interacting with cascading drop-downs.
  The only rudimentary fix I have successfully employed is "Thread.Sleep"... See code extract below:</p>
  
  <p>Note I am passing the following parameters:</p>
  
  <p>attribute: ID</p>
  
  <p>attrval: e.g. ID123456 (ID of the drop-down)</p>
  
  <p>parameter: Car (drop-down value we are wanting to select)</p>
  
  <pre><code>IWebElement element = findMyElement(attribute, attrval);
  SelectElement selectElement = new SelectElement(element);
  selectElement.SelectByText(parameter);
  // dirty code - needs to be re-written
  Thread.Sleep(500);
  if (new SelectElement(findMyElement(attribute, attrval)).SelectedOption.Text.Equals(parameter))
  {
      return "pass";
  }
  </code></pre>
  
  <p>Note2: findMyElement is a custom method (here is an extract):</p>
  
  <pre><code>public static IWebElement findMyElement(string attribute, string attrval)
          {
              WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
              switch (attribute.ToLower())
              {
                  case "id":
                      wait.Until(ExpectedConditions.ElementExists(By.Id(attrval)));
                      wait.Until(ExpectedConditions.ElementIsVisible(By.Id(attrval)));
                      return driver.FindElement(By.Id(attrval));
  </code></pre>
  
  <p>As I have stated in my code comments is there anyway I can avoid using <strong>Thread.Sleep</strong> as I am aware that this is not a recommended approach.</p>
  
  <p>Thanks in advance :)</p>