1

我正在使用 selenium 在多个浏览器上运行我的 TestNG 测试。我有一个名为“start”的方法,其中包括浏览器的所有分配。这是它的外观。

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

}

所以当我运行它时,它只在 Firefox 中有效,有时在 Chrome 和 IE 11 中有效。但在 Chrome 和 IE 11 中,它偶尔会随机失败。我每次都会遇到不同类型的异常。

我在线程“main”org.openqa.selenium.StaleElementReferenceException 中得到----异常:过时的元素引用:元素未附加到页面文档----在 chrome 和 IE11 中。

它发生得非常随机,我认为它与 html DOM 有关。我能做些什么来防止这种情况发生?

4

1 回答 1

0

尝试添加更多同步。例如,

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

,就在您导航到该页面之后并且在您与页面上的任何内容进行交互之前。

于 2015-08-07T08:50:26.313 回答