简体   繁体   中英

Chrome and Internet Explorer Selenium web drivers fail randomly

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.

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();
    }

}

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.

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.

It happens very randomly and I think it has something to do with html DOM. What is something I can do to prevent this?

Try adding in some more synchronization. For example,

      WebDriverWait wait = new WebDriverWait(driver,10); 
      wait.until(presenceOfElementLocated(By.id("login"));

, just after you navigate to the page and before you interact with anything on the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM