我正在使用硒在多个浏览器上运行TestNG测试。我有一种称为“启动”的方法,其中包括浏览器的所有分配。这里看起来如何。

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中,它偶尔会偶然失效。我每次都会得到不同类型的异常。

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

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

最佳答案

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

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


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

09-12 15:23