【问题标题】:Webdriver wait not throwing timeout exceptionWebdriver等待不抛出超时异常
【发布时间】:2018-04-18 06:42:40
【问题描述】:

您好,我正在尝试在硒测试中使用 webdriver。我想检查它是如何工作的。我给了 5 秒作为 webdriver 等待的最长时间。我的页面加载时间超过 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

【问题讨论】:

  • 尝试使用 try-catch。在under catch中,使用'throw'语句。
  • 我必须把 try catch 放在哪里
  • @learner 您预计哪行代码会出现错误? selenium 加载页面和 webdriverwait 是有区别的...
  • 我期待登录按钮代码中出现超时异常..bcoz 有时我的初始网址需要超过 60 秒才能加载..加载初始网址后第一页包含登录按钮..

标签: selenium selenium-webdriver webdriver timeout webdriverwait


【解决方案1】:

我已经用 try-catch 修改了方法。请尝试以下方法:

public static void main(String[] args) throws Exception {
try{
    executeTest();
    } catch (Exception e) {
        System.out.println(e);
        throw e;
    }
}
private static void executeTest( ) throws Exception {
try{

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

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

    WebDriverWait wait = new WebDriverWait(driver, 5);

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

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

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

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

    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();
    }
    catch(Exception e) {
        System.out.println(e);
        throw e;
    }
}

【讨论】:

  • 按你说的试过了,但没有收到任何效果,只显示加载时间。仍然加载时间超过 7 秒
  • 请尝试更改。我刚刚宣布在driver.get() 之前等待。
  • 仍然没有出现任何异常,最后加载时间是“加载时间7842,加载时间9983”..
【解决方案2】:

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

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

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

    如果您不配置pageLoadTimeout(),根据GeckoDriver的当前实现,考虑默认值"pageLoad":300000

    您可以在pageLoadTimeout in Selenium not working中找到关于pageLoadTimeout()的详细讨论

  • WebDriverWait() : WebDriverWait()FluentWait 的特化,它使用 WebDriver 实例并与 ExpectedConditions 类结合使用,该类被定义为等待特定条件发生,然后再继续代码。

    在您的代码中,您已将 WebDriverWait 配置为 5 秒,这适用于您用作的所有 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")));
    

    所有ExpectedConditions都在5秒的时间跨度内实现。所以你也看不到任何异常/错误。

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

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

【讨论】:

  • 但是在我的代码页LoadTimeout() 中被注释了,我没有使用那个代码..当我给webdriver 等待1 秒时,当时也没有得到任何异常..
  • 更新了我的答案如果不配置pageLoadTimeout(),按照GeckoDriver的当前实现,"pageLoad":300000的默认值为考虑
  • k. 你能在 webdriver 中说当我给 1 秒时等待吗,当时我也没有得到任何异常......我厌倦了许多测试运行......并且加载元素花费了超过 1 秒,den它也没有抛出任何异常。所以我很困惑
  • 很大程度上取决于AUT(被测应用程序)的架构。如果应用程序基于 HTML5,通常所有元素将在 WebClient 达到 'document.readyState' is equal to "complete" 但在 Angular 的情况下加载基于 React Native 的应用程序 WebDriverWait() 起着至关重要的作用。在您的情况下 WebDriverWait() 可能是微不足道的,但绝对是前进的好习惯。
  • @learner 如果我的Answer满足了您的Question,请点击空心处AcceptAnswer我的 Answer 旁边的复选标记位于 VoteDown 箭头下方,因此复选标记变为 green
猜你喜欢
  • 2016-09-16
  • 2013-02-22
  • 2018-07-14
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
相关资源
最近更新 更多