当Selenium抛出pageLoadTimeout异常时,如何删除块?

时间:2013-07-25 05:04:11

标签: java firefox selenium-webdriver

我正在将一个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请求通过

更新:当我启用堆栈跟踪时抛出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)

2 个答案:

答案 0 :(得分:0)

您可以使用try catch块作为driver.get()代码。在catch中,您实际上可以打印页面未加载的URL。不要在catch块中打印堆栈跟踪。这将再次停止执行。

答案 1 :(得分:0)

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;
            }

        }

    }
}