我正在将一个uris列表加载到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请求通过
更新:当我启用stacktrace时抛出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)I'm loading a list of uris into a Java program, and then for each uri, I use selenium to navigate to the page and perform some analysis. However, I'd like to limit the wait time and just skip pages if they're taking too long to load. For example, I navigate to google and it takes like 5 seconds. But then I navigate to some random Chinese site and 30 seconds passes and the server still hasn't responded- Firefox just sits there spinning that little waiting circle.
Selenium appears to throw an exception if I use pageLoadTimeout, but it still sits there for the server to return something before moving on. I'd like to just skip it at the moment it hits the timeout, because otherwise the timeout is quite useless.
Here is the entire bit of code:
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(); } }Note I set the timeout to 3 milliseconds to be ridiculous for testing purposes so it would timeout on just about every site. It throws the exception, but then still just sits there waiting for the GET request to go through
UPDATE: Here is the stacktrace when it throws the TimeoutException, when I enable the stacktrace
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)最满意答案
public class Scout { public static void main(String[] args) { ProfilesIni allProfiles = new ProfilesIni(); FirefoxProfile profile = allProfiles.getProfile("SeleniumProfile"); WebDriver driver = null; 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 { if(driver == null) { driver = new FirefoxDriver(); driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS); } driver.get(item); } catch (Exception e) { System.out.println("Did not get url"); driver.quit(); driver = null; } } } } public class Scout { public static void main(String[] args) { ProfilesIni allProfiles = new ProfilesIni(); FirefoxProfile profile = allProfiles.getProfile("SeleniumProfile"); WebDriver driver = null; 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 { if(driver == null) { driver = new FirefoxDriver(); driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS); } driver.get(item); } catch (Exception e) { System.out.println("Did not get url"); driver.quit(); driver = null; } } } }更多推荐
发布评论