在Selenium中等待页面完全加载

4

我正在使用Java的selenium。在执行页面上的任何操作之前,我希望等待页面完全加载。

我尝试过以下方法,但它不能按预期工作。

public void waitForElementToBeVisible(final WebElement element) {

    WebDriverWait wait = new WebDriverWait(WebDriverFactory.getWebDriver(), WEBDRIVER_PAUSE_TIME);

    wait.until(ExpectedConditions.visibilityOf(element));

这可能与以下内容相关:https://dev59.com/WmUp5IYBdhLWcg3wrY9p - René Jahn
可能是等待Selenium中的页面加载的重复问题。 - The Bearded Llama
这个元素是页面上最后加载的吗?页面上是否有动态的事情发生,使得在所有东西似乎都加载完成后仍然会不断地移动?您是否收到关于元素不存在或元素过时的错误? - mrfreester
不,我想点击的元素不是最后加载的,但我认为最好在它完全加载后执行任何操作,请纠正我如果我错了,因为我是自动化新手,如果有人能提供更多关于Java和Selenium的了解的好链接将不胜感激,谢谢。 - user6939
澄清问题。 - Lucas Holt
6个回答

5

0

这里是针对处理Angular 7或8时的终极解决方案。

与使用sleep或implicit wait方法等待更长时间不同,您可以将等待时间分成分区并递归使用它。

以下逻辑将等待页面渲染至少300秒,最多900秒。

/**
 * This method will check page loading
 *
 */
public void waitForLoadingToComplete() {
    waitLoadingTime(3); // Enter the number of attempts you want to try
}

 private void waitLoadingTime(int i) {
        try {
  // wait for the loader to appear after particular action/click/navigation
            this.staticWait(300); 
  // check for the loader and wait till loader gets disappear
            waitForElementToBeNotPresent(By.cssSelector("Loader Element CSS"));                                                             
        } catch (org.openqa.selenium.TimeoutException e) {
            if (i != 0)
                waitLoadingTime(i - 1);
        }
    }

/**
 * This method is for the static wait
 *
 * @param millis
 */
public void staticWait(final long millis) {
    try {
        TimeUnit.MILLISECONDS.sleep(millis);
    } catch (final InterruptedException e) {
        System.err.println("Error in staticWait." + e);
    }
}

public void waitForElementToBeNotPresent(final By element) {
    long s = System.currentTimeMillis();
    new WebDriverWait(this.getDriver(), 30)
            .until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(element)));
    System.err.println("Waiting for Element to be not present completed. " + (System.currentTimeMillis() - s));
}

0

此方法将等待元素可见。 首先,此方法将检查元素是否在HTML中可用以及它是否显示... 它将等待元素显示...

public void E_WaitUntilElementDisplay() throws Exception
{
    int i=1;
    boolean eleche,eleche1 = false; 
    while(i<=1)
    {
            try{
                eleche = driver.findElements(by.xpath("path")).size()!=0;
            }catch(InvalidSelectorException ISExcep)
            {
                eleche = false;
            }
            if(eleche == true)
            {

                while(i<=1)
                {
                    try{
                        eleche1=driver.findElement(By.xpath("Path")).isDisplayed();
                    }catch(org.openqa.selenium.NoSuchElementException NSEE){
                        eleche1=false;
                    }
                    if(eleche1 == true)
                    {
                        i=2;
                        System.out.println("\nElement Displayed.");
                    }
                    else
                    {
                        i=1;
                        Thread.sleep(1500);
                        System.out.println("\nWaiting for element, to display.");
                    }
                }
            }
            else
            {
                i=1;
                Thread.sleep(1500);
                System.out.println("\nWaiting for element, to display.");
            }
    }
}

0


作为另一种选择,也许你可以尝试类似这样的东西:

if(element.isDisplayed() && element.isEnabled()){
   //your code here 
}

或者如果您知道要等多久:

thread.sleep(3000); //where 3000 is time expression in milliseconds, in this case 3 secs

0

您可以在Java中使用此函数来验证页面是否已完全加载。验证过程是双重的。一种是使用JavaScript document.readystate,另一种是对JavaScript施加等待时间。

/* Function to wait for the page to load. otherwise it will fail the test*/
public void waitForPageToLoad() {
    ExpectedCondition<Boolean> javascriptDone = new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            try {
                return ((JavascriptExecutor) getDriver()).executeScript("return document.readyState").equals("complete");
            } catch (Exception e) {
                return Boolean.FALSE;
            }
        }
    };
    WebDriverWait wait = new WebDriverWait(getDriver(), waitTimeOut);
    wait.until(javascriptDone);
}

0
这对我有效:
wait.until(ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//div[contains(text(),'Loading...')]"))));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接