我们可以在selenium中使用ExpectedConditions和FluentWait进行通用的等待方法吗?

时间:2013-10-23 09:23:12

标签: java selenium selenium-webdriver selenium-grid

我们可以有一个通用的等待方法......即(1.等待页面加载2.找到元素3.如果没有找到刷新),这一直持续到fluentwait超时。

2 个答案:

答案 0 :(得分:1)

你可以使用 WebdriverWait - (1.等待页面加载2.找到元素3.如果没有找到刷新)

   WebDriverWait wait = new WebDriverWait(driver, 18); // Times Out after 18 Seconds
    PageUtil.refreshObject(driver, By.linkText("Element")); // refresh the Element
    wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Element"))); // wait till  Element is Enabled and Visible before Clicking on that Element

    /**
    *Pass the Control to that Element and Click on that Element (preferred in IE)
    *
    */
    WebElement element = driver.findElement(By.linkText("Element"));
    element.sendKeys(org.openqa.selenium.Keys.CONTROL);
    element.click();

以下是 RefreshObject 方法, PageUtil 是ClassName

public static WebElement refreshObject(WebDriver driver, By locator) {
        int counter = 0;

        try {
            counter = counter + 1;

            return driver.findElement(locator);
        }

        catch (StaleElementReferenceException e) {

            return refreshObject(driver, locator);

        }

    }

答案 1 :(得分:0)

你有特殊的商业案例需要这个吗?如果不这样做,则可以在设置驱动程序对象时设置隐式等待。在c#中它看起来像:

myDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

很多人会说你应该避免隐式等待,但是如果你觉得你需要建立一个方便的方法,那么隐含的等待是可取的。

相关问题