我正在将 uri 列表加载到 Java 程序中,然后对于每个 uri,我使用 selenium 导航到页面并执行一些分析。但是,我想限制等待时间,如果加载时间过长,就跳过页面。例如,我导航到谷歌,它需要大约 5 秒。但是后来我导航到一些随机的中文网站,30 秒过去了,服务器仍然没有响应——Firefox 只是坐在那里旋转那个小等待圈。
如果我使用 pageLoadTimeout,Selenium 似乎会抛出异常,但它仍然存在,以便服务器在继续之前返回某些内容。我想在它达到超时的那一刻跳过它,因为否则超时是毫无用处的。
这是整个代码:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.concurrent.TimeUnit;
public class scout {
public static void main(String[] args) {
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("SeleniumProfile");
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().pageLoadTimeout(3,TimeUnit.MILLISECONDS);
BufferedReader in = null;
List<String> myList = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("scoutLinks.txt"));
String str;
while ((str = in.readLine()) != null) {
myList.add(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for(String item : myList ){
try {
driver.get(item);
} catch (Exception e) {}
}
//Quit
//driver.quit();
}
}
请注意,我将超时设置为 3 毫秒,这对于测试目的来说是荒谬的,因此它几乎会在每个站点上超时。它抛出异常,但仍然只是坐在那里等待 GET 请求通过
更新:当我启用堆栈跟踪时,这是抛出 TimeoutException 时的堆栈跟踪
org.openqa.selenium.TimeoutException: Timed out waiting for page load.
Command duration or timeout: 56 milliseconds
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.ver
sion: '1.7.0_10'
Session ID: e7c34ae4-027c-4bd4-974e-f314b9aa5c74
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=22.0}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:276)
at scout.main(scout.java:36)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Timed out waiting for page load.
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_10'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.get/<(file:///C:/Users/Nate/AppData/Local/Temp/anonymous7949477209431295646webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8312)
at <anonymous class>.WebLoadingListener/e(file:///C:/Users/Nate/AppData/Local/Temp/anonymous7949477209431295646webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:3263)
at <anonymous class>.WebLoadingListener/<(file:///C:/Users/Nate/AppData/Local/Temp/anonymous7949477209431295646webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:3270)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Nate/AppData/Local/Temp/anonymous7949477209431295646webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:386)