0

ループしてそれぞれをクリックする必要がある webElements のリストがありますが、クリックするたびにページが更新されるため、StaleElementReferenceException が発生します。各要素は次のようになります。

<img src="images/english/edit.gif" border="0" height="24" width="47">

そこで、再帰的な方法を使用して各 webElement をクリックし、インデックスを次のインスタンスに渡し、リストを更新します。


public int enterDescription(int place) { リストの説明 = driver.findElements(By.cssSelector(img[src='images/english/edit.gif']));

for (int index = 0; index < descriptions.size(); index++) { index = place; if(place==descriptions.size()) { return place; } else { descriptions.get(index).click(); enterDescription(place++); } } return place; }

これは、最初はメソッドがクラッシュする終了条件まで機能し、終了条件に到達すると、すべてのインスタンスを一度に終了する必要があります。何か案は?

4

2 に答える 2

0

再帰的なものを無視して、この構造で終了したいというあなたの願いを達成します

public interface Terminatable {
    void terminate();
}

public class Terminator {
    private LinkedList<Terminatable> terminatables = new LinkedList<Terminatable>();

    public void register(Terminatable terminatable) {
        terminatables.offer(terminatable);
    }

    public void unregister(Terminatable terminatable) {
        terminatables.remove(terminatable);
    }

    public void terminate() {
        Terminatable terminatable = terminatables.poll();

        while(terminatable != null) {
            terminatable.terminate();
            terminatable = terminatables.poll();
        }
    }
}

public class Worker implements terminatable {
    private Terminator terminator;

    public Worker(Terminator terminator) {
        this.terminator = terminator;
        terminator.register(this);
    }

    public void terminate() {
        // do your termination stuff here
    }

    [...]

    public void work() {
        // do your work and start termination when you are finished
        terminator.terminate();
    }
}

ワーカーが不要になった場合は、忘れずにワーカーの登録を解除してください。

于 2013-04-19T10:40:23.560 に答える