Webdriver等待不抛出超时异常

时间:2018-04-18 06:42:40

标签: selenium selenium-webdriver webdriver timeout webdriverwait

  

嗨,我正在尝试在selenium测试中使用webdriver。我想检查它是如何工作的。在webdriver等待中我给了5秒作为最长时间。我的页面加载时间超过7秒,但我仍然没有从webdriver等待获得任何超时异常。我也给我的控制台输出。请告诉我为什么我没有超时例外?

public class MainClass {

private static ChromeDriver driver;


public static void main(String[] args) {



            executeTest();

}

private static void executeTest( )  {

    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

    driver = new ChromeDriver();
    // driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

    driver.get("http://myUrl");

    Long loadtime = (Long) ((JavascriptExecutor) driver)
            .executeScript("return performance.timing.loadEventEnd - performance.timing.navigationStart;");

    System.out.println("loadding time " + Loadtime);

    WebDriverWait wait = new WebDriverWait(driver, 5);

    Boolean sign_in = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton")))
            .isDisplayed();

    if (sign_in == true) {
        driver.findElement(By.id("signinbutton")).click();
    } else {
        System.out.println("Oops! Couldn't locate sign_in element!");
    }

    Boolean user_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id")))
            .isDisplayed();
    // user_name.sendKeys("ANAND@RIL");
    if (user_name == true) {
        driver.findElement(By.id("username_id")).sendKeys("ANAND@RIL");
    } else {
        System.out.println("Oops! Couldn't locate user_name element!");
    }

    Boolean password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id")))
            .isDisplayed();

    if (password == true) {
        driver.findElement(By.id("password_id")).sendKeys("ANAND");
    } else {
        System.out.println("Oops! Couldn't locate password element!");
    }

    WebElement login = 
    wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton")));
    login.click();

}
}

控制台输出:

尝试1:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 42020
Only local connections are allowed.
Apr 18, 2018 12:07:35 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loading time 7872**

尝试2:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 38325
Only local connections are allowed.
Apr 18, 2018 12:07:46 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loadding time 6632**

尝试3:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 34619
Only local connections are allowed.
Apr 18, 2018 12:07:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loadding time 7522**

尝试4:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 48000
Only local connections are allowed.
Apr 18, 2018 12:08:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS

2 个答案:

答案 0 :(得分:1)

我用try-catch修改了方法。请尝试下面的事情:

-o calc.tab.cc

答案 1 :(得分:1)

您需要考虑以下几个事实:

  • pageLoadTimeout()pageLoadTimeout()设置在抛出异常/错误之前等待页面加载完全的时间。如果超时为负,则页面加载可能是无限期的。

    在您的代码中,您已将pageLoadTimeout()配置为 40 秒。根据日志,您的网页加载时间 7872 ms 6632 ms 7522 ms ,因此您不会看到异常/错误。

    如果您未配置WebDriverWait(),则根据 GeckoDriver 的当前实施情况,默认值&#34; pageLoad&#34;:300000 < / strong>被认为是。

    您可以在pageLoadTimeout in Selenium not working

  • 中找到关于 pageLoadTimeout()的详细讨论
  • WebDriverWait()WebDriverWaitFluentWait的特化,它使用WebDriver实例并与ExpectedConditions类结合使用,该类定义为等待在进一步处理代码之前要发生的某些条件。

    在您的代码中,您已将 wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton"))); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id"))); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id"))); wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton"))); 配置为 5 秒,这适用于您用作的所有 ExpectedConditions

    {{1}}

    所有 ExpectedConditions 都是在 5 秒的时间范围内实现的。所以你也没有看到任何异常/错误。

    您可以在Replace implicit wait with explicit wait (selenium webdriver & java)

  • 中找到关于 WebDriverWait / ExplicitWait 的详细讨论