How to wait for page redirection in Selenium?

I am trying to accomplish a relatively simple task: wait for the page to redirect. Just seen another one answered a question on this question, where should the council wait for a certain text to appear on the last page (IF I understood correctly). If so, what about the wait before window.locationbeing changed? This is better? Worse? Less applicable? Any other idea? just curious, if necessary, may mark this issue as a community wiki.

Thank!

+5
source share
3 answers

, driver.get_location . -, , 5 , , , window.location.href .

+1

, Selenium. , . -, . , :

Actions builder = new Actions( driver );
builder.click( driver.findElement( By.className("lala") ) ).perform();

, , , , "lala". :

driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );

5 . - 5 , . , . , . , .

, GetElementByClassAndText, , , , , , :

public static void waitAndClick( WebDriver driver, By by, String text ) {
    WebDriverWait wait = new WebDriverWait( driver, 10000 );
    Function<WebDriver, Boolean> waitForElement = new waitForElement( by );
    wait.until( waitForElement );

    for( WebElement e : driver.findElements( by ) ) {
        if( e.getText().equals( text ) ) {
            Actions builder = new Actions( driver );
            builder.click( e ).perform();
            return;
        }
    }
}

, :

public class waitForElement implements Function<WebDriver, Boolean> {
    private final By by;
    private String text = null;

    public waitForElement( By by ) {
        this.by = by;
    }

    public waitForElement( By by, String text ) {
        this.by   = by;
        this.text = text;
    }

    @Override
    public Boolean apply( WebDriver from ) {
        if( this.text != null ) {
            for( WebElement e : from.findElements( this.by ) ) {
                if( e.getText().equals( this.text ) ) {
                    return Boolean.TRUE;
                }
            }

            return Boolean.FALSE;
        } else {
            try {
                from.findElement( this.by );
            } catch( Exception e ) {
                return Boolean.FALSE;
            }

            return Boolean.TRUE;
        }
    }
}

, Selenium Ruby, , , ( , ) .

+1

, ( ): driver.getCurrentUrl();

-3

All Articles