title: Chrome and Internet Explorer Selenium web drivers fail randomly
body: <p>I am running my TestNG tests on multiple browsers using selenium. I have this method called "start" that includes all the assignments of browsers. Here how it looks. </p>
  
  <pre><code>public class Main {
  
      public static  WebDriver driver;
  
      public static void main(String[] args) throws MalformedURLException {
          start("localchrome");
          menuSelector("Grants", "Make a ", "Drive");
          quit();
      }
  
      @Parameters("browser")
      public static void start (String browsername) throws MalformedURLException {
          DesiredCapabilities capability;
  
          if(browsername.equalsIgnoreCase("firefox")){
              capability = DesiredCapabilities.firefox();
              driver = new RemoteWebDriver(new URL("http://###.###.##.####:4441/wd/hub"), capability);
              capability.setBrowserName("firefox");
          }
  
          else if (browsername.equalsIgnoreCase("chrome")){
              capability = DesiredCapabilities.chrome();
              driver = new RemoteWebDriver(new URL("http://###.###.##.####:4441/wd/hub"), capability);
              capability.setBrowserName("chrome");
          }
  
          else if (browsername.equalsIgnoreCase("ie")){
              capability = DesiredCapabilities.internetExplorer();
              capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
              driver = new RemoteWebDriver(new URL("http://###.###.##.####:4441/wd/hub"), capability);
              capability.setBrowserName("internet explorer");
          }
  
          else if (browsername.equalsIgnoreCase("LocalChrome")){
              System.setProperty("webdriver.chrome.driver", "/Users/#######/Documents/chromedriver");
              driver = new ChromeDriver();
          }
  
          else if (browsername.equalsIgnoreCase("LocalFirefox")){
              ProfilesIni profilesIni = new ProfilesIni();
              FirefoxProfile mp = profilesIni.getProfile("myProfile");
              driver = new FirefoxDriver(mp);
          }
  
          driver.navigate().to("http://www.someWebsite.com/login");
  
          WebElement loginInput = driver.findElement(By.id("login"));
          loginInput.sendKeys("user1");
          WebElement passwordInput = driver.findElement(By.id("pass"));
          passwordInput.sendKeys("pass111");
          WebElement loginButton = driver.findElement(By.id("submit"));
          loginButton.submit();
          WebElement nameField = driver.findElement(By.name("value(name)"));
          nameField.sendKeys("Acc 123");
          WebElement goButton = driver.findElement(By.name("value(search)"));
          goButton.click();
  
          WebElement selectButton = driver.findElement(By.name("select.name"));
          selectButton.click();
      }
  
      public static void quit(){
          driver.quit();
      }
  
      public static void menuSelector(String a, String b, String c){
  
          WebElement menuItem = driver.findElement(By.xpath("//td/div[contains(.,'"+a+"')]"));
          Actions actions = new Actions(driver);
          actions.moveToElement(menuItem).perform();
  
          WebDriverWait wait = new WebDriverWait(driver, 60);
          WebElement subMenuItem = wait.until(ExpectedConditions.
                  visibilityOfElementLocated
                          (By.xpath("//td[contains(.,'" + b + "') and @class='label']")));
  
          actions.moveToElement(subMenuItem).perform();
  
          WebElement subItem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(.,'"+c+"') and @class='label']")));
          actions.moveToElement(subItem).click().perform();
      }
  
  }
  </code></pre>
  
  <p>So when I run this, it only works in Firefox, and sometimes in Chrome and IE 11. But in Chrome and IE 11 it randomly fails once in awhile. I get different type of exceptions every time. </p>
  
  <p>I get ----Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document---- in chrome and IE11.</p>
  
  <p>It happens very randomly and I think it has something to do with html DOM. What is something I can do to prevent this?</p>