selenium with PhantomJs wait till page fully loaded?

99封情书 提交于 2019-12-04 06:11:24

For your "Explicit wait:" option, I think the correct sequence should be:

1) Navigate to target url by:

driver.Navigate().GoToUrl(url);

2) Wait until the target url fully loaded by

IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

In this way next line will wait page fully loaded before read PageSource.

I have created a extension method. In this method you can put your condition.

  public static bool WaitUntil(this IWebDriver driver, Func<IWebDriver, bool> expression, int timeOutSeconds = 10)
    {

        TimeSpan timeSpent = new TimeSpan();

        bool result = false;
        while (timeSpent.TotalSeconds < timeOutSeconds)
        {

            result = expression.Invoke(driver);

            if (result == true)
            {
                break;
            }
            Thread.Sleep(timeSleepingSpan);
            timeSpent = timeSpent.Add(new TimeSpan(0, 0, 0, 0, timeWaitingSpan));

        }
        return result;

    }

Like

       driver.WaitUntil(d => d.Url.Equals("https://www.meusite.com/"));

Try something like this:

try (
  ExpectedConditions.presenceOfElementLocatedBy
  ExpectedConditions.visibilityOfElementLocatedBy
) catch error if both conditions are not met
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!