0

URI のリストを Java プログラムにロードし、各 URI に対してセレンを使用してページに移動し、分析を実行します。ただし、待ち時間を制限し、読み込みに時間がかかりすぎる場合はページをスキップしたいと思います. たとえば、Google に移動すると、5 秒ほどかかります。しかし、ランダムな中国のサイトに移動して 30 秒が経過しても、サーバーはまだ応答しません。

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)
4

2 に答える 2

0

driver.get() コードに try catch ブロックを使用できます。キャッチでは、ページが読み込まれなかった URL を実際に出力できます。catch ブロックでスタック トレースを出力しません。これにより、実行が再び停止します。

于 2013-07-25T05:08:47.520 に答える
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;
            }

        }

    }
}
于 2013-07-25T08:25:26.600 に答える