Selenium Webdriver等待未引发超时异常
您好,我正在尝试在selenium测试中使用webdriver。我想看看它是怎么工作的。我给了5秒作为webdriver等待的最长时间。我的页面加载时间超过7秒,但我仍然没有从webdriver wait获得任何超时异常。我也给出了我的控制台输出。请告诉我为什么没有收到超时异常 控制台输出: 尝试1:Selenium Webdriver等待未引发超时异常,selenium,selenium-webdriver,webdriver,timeout,webdriverwait,Selenium,Selenium Webdriver,Webdriver,Timeout,Webdriverwait,您好,我正在尝试在selenium测试中使用webdriver。我想看看它是怎么工作的。我给了5秒作为webdriver等待的最长时间。我的页面加载时间超过7秒,但我仍然没有从webdriver wait获得任何超时异常。我也给出了我的控制台输出。请告诉我为什么没有收到超时异常 控制台输出: 尝试1: Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 42020 Only lo
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;
}
}
我用一个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()
配置为40秒。根据日志,您的页面加载时间为7872 ms、6632 ms和7522 ms,因此您没有看到异常/错误 如果未配置pageLoadTimeout()
,则根据GeckoDriver的当前实现,将考虑默认值“pageLoad”:300000 您可以在中找到关于pageLoadTimeout()的详细讨论pageLoadTimeout()
- :
是的专门化,它使用WebDriver实例,并与类一起工作,该类被定义为在继续代码之前等待特定条件发生 在代码中,您已将WebDriverWait()
配置为5秒,这适用于您使用的所有预期条件:WebDriverWait
所有预期条件均在5秒的时间范围内达到。因此,您也不会看到任何异常/错误 您可以在中找到关于WebDriverWait/ExplicitWait的详细讨论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")));
你需要考虑以下几个事实:
- :
设置在引发异常/错误之前等待页面加载完成的时间量。如果超时为负,则页面加载可能是不确定的 在代码中,您已将pageLoadTimeout()
配置为40秒。根据日志,您的页面加载时间为7872 ms、6632 ms和7522 ms,因此您没有看到异常/错误 如果未配置pageLoadTimeout()
,则根据GeckoDriver的当前实现,将考虑默认值“pageLoad”:300000 您可以在中找到关于pageLoadTimeout()的详细讨论pageLoadTimeout()
- :
是的专门化,它使用WebDriver实例,并与类一起工作,该类被定义为在继续代码之前等待特定条件发生 在代码中,您已将WebDriverWait()
配置为5秒,这适用于您使用的所有预期条件:WebDriverWait
所有预期条件均在5秒的时间范围内达到。因此,您也不会看到任何异常/错误 您可以在中找到关于WebDriverWait/ExplicitWait的详细讨论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")));
driver.get()
。仍然没有得到任何异常,最后一次加载时间是“加载时间7842,加载时间9983”。正如你所说的那样进行了尝试,但没有得到任何eception,只显示加载时间。加载时间仍然在7秒以上。请尝试更改。我刚刚声明了wait beforedriver.get()
。仍然没有得到任何异常,最后一次加载时间是“加载时间7842,加载时间9983”。但在我的代码页面loadTimeout()被注释,我没有使用该代码..当我给webdriver等待1秒时,当时我也没有收到任何异常..更新了我的答案,如果你没有配置pageLoadTimeout()
,根据GeckoDriver的当前实现,默认值“pageLoad”:300000被认为是dk。当我给你1秒的时间,你能说在webdriver中等待吗,在那个时候,我也没有得到任何异常。我厌倦了许多测试运行。加载elemets花费了超过1秒的时间,而且它也没有引发任何异常。因此,我很困惑很多取决于AUT(被测应用程序)的体系结构。如果应用程序通常基于HTML5,则当WebClient实现“document.readyState”等于“complete”时,所有元素都将加载,但对于Angular或React本机应用程序,WebDriverWait()起着至关重要的作用。在您的情况下,WebDriverWait()可能很琐碎,但向前看肯定是一个很好的做法。@学习者如果我的答案符合您的问题,请单击我的答案旁边的空心复选标记(位于VoteDown箭头下方)来输入答案,以便