嗨,我正在尝试在硒测试中使用网络驱动程序。我想检查它是如何工作的。我给了 5 秒作为网络驱动程序等待的最长时间。我的页面加载时间超过 7 秒,但我仍然没有收到来自网络驱动程序等待的任何超时异常。我也在提供我的控制台输出。请告诉我为什么我没有收到超时异常?
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 修改了该方法。请尝试以下操作:
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;
}
}
您需要考虑以下一些事实:
-
pageLoadTimeout()
:pageLoadTimeout()
设置在引发异常/错误之前等待页面加载完全的时间量。如果超时为负,则页面加载可能是无限期的。在代码中,已将
pageLoadTimeout()
秒配置为40秒。根据日志,您的页面加载时间为7872毫秒、6632毫秒和7522 毫秒,因此您不会看到异常/错误。如果不配置
pageLoadTimeout()
,则根据GeckoDriver的当前实现,则考虑默认值"pageLoad":300000。您可以在pageLoadTimeout()中找到详细的讨论pageLoadTimeout in Selenium 不起作用
-
WebDriverWait()
:WebDriverWait()
是 FluentWait 的专用化,它使用 WebDriver 实例并与ExpectConditions类结合使用,该类被定义为等待某个条件发生,然后再继续代码。在您的代码中,您已经配置了
WebDriverWait
到5秒,这适用于您用作的所有预期条件: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秒的时间跨度内实现。因此,您也不会看到任何异常/错误。
您可以在将隐式等待替换为显式等待(selenium webdriver & java)中找到有关WebDriverWait/ExplicitWait的详细讨论