Selenium Wait for Source Source contains

I'm just wondering if there is an elegant way to use, ExpectedConditionsor something else, so that my code expects the page source to contain a given string before a certain timeout. I know that I can use something like this if I want to use a specific element locator ...

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("foobar")));

But I wanted to accomplish this without using a locator for a specific element, and just use the entire page source as a link. Any suggestions?

+4
source share
3 answers

. weddriver . HTML-, . JavaScript. - JavaScript - HTML.

, , .

wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by)));

wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(element))h;
+2

, DOM:

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("html")));

, ExpectedCondition<T> . WebDriver (- - com.google.common.base.Function<WebDriver,T>).

apply WebDriver getPageSource(), String, . String , .

0

You can wait for the document to readyStatebecome complete. Run javascript return document.readyState").equals("complete")on the downloadable webpage.

void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new
        ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);
}

And then you can get the page source:

driver.getPageSource();

And then make sure that pageSource contains what you are looking for:

driver.getPageSource().contains("your element/tag");

Hope this helps!

0
source

All Articles