0

I am using selenium, IEDriver, and C# and I would like to wait for the page to load. I have this code:

/// <summary>
/// Ceka dokud neni stranka nastena
/// </summary>
public static void WaitForPageToLoad()
{
  try
  {
    Thread.Sleep(500);
    Log.Trace("Browser.WaitForPageToLoad() - Ceka dokud neni stranka nactena ...");
    new WebDriverWait(Browser.Driver, new TimeSpan(0, 0, 360)).Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
    Thread.Sleep(1000);
  }
  catch (Exception ex)
  {
    Log.Error(ex);
    throw;
  }
}

but it will crash on this:

    2018-01-04 15:39:27.2266 - ERROR: System.InvalidOperationException: JavaScript error (UnexpectedJavaScriptError)
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
   at BaseFramework.Browser.<>c.<WaitForPageToLoad>b__10_0(IWebDriver d) in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
   at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
   at BaseFramework.Browser.WaitForPageToLoad() in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
EXCEPTION: System.InvalidOperationException: JavaScript error (UnexpectedJavaScriptError)
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
   at BaseFramework.Browser.<>c.<WaitForPageToLoad>b__10_0(IWebDriver d) in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
   at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
   at BaseFramework.Browser.WaitForPageToLoad() in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 103
   at Gamma.Tests.GammaICRM.AT82688_ICRM_SMOKE() in C:\TFS\PRIVPMT\Selenium\Gamma.UI.Tests\Gamma.Tests\GammaICRM.cs:line 72
   at Gamma.Tests.GammaICRM.AT82688_ICRM_SMOKE_PerformTest() in C:\TFS\PRIVPMT\Selenium\Gamma.UI.Tests\Gamma.Tests\GammaICRM.cs:line 23

most of the time it works, but from time to time it will crash on this

next method is this:

public static void LeftClick(this IWebElement element)
    {
      //pockame dokud nelze na element kliknout
      new WebDriverWait(Browser.Driver, new TimeSpan(0, 0, 120)).Until(ExpectedConditions.ElementToBeClickable(element));

      Actions actions = new Actions(Browser.Driver);
      //posuneme cursor na element
      actions.MoveToElement(element).Perform();
      //klikeneme na element
      actions.Click().Build().Perform();
    }

passed webElement is founded by XPath (webElement is always correct)

Podlipny
  • 75
  • 3
  • 9

2 Answers2

0

You could try calling the WebDriverWait class. You can use this to wait for an element to show up on the page (i.e the page loads if this object exists)

public static IWebElement WaitUntilElementExists(By elementLocator, int timeout = 10)
{
    try
    {
        var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
        return wait.Until(ExpectedConditions.ElementExists(elementLocator));
    }
    catch (NoSuchElementException)
    {
        Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
        throw;
    }
}

In this code the method returns wait.until then the elementexists condition on an element that is supposed to be on the page. Just have it try to find an element on the page which is supposed to be there when the page loads. If it doesn't exist for a certain amount of time it will give the NoSuchElementException (so a timeout). Catch this and give your own output

source: How to get webDriver to wait for page to load (C# Selenium project)

hope this helps at all

L_Church
  • 703
  • 1
  • 8
  • 19
0

I've faced the same issue and the problem was that I was inside of a frame. Switching back to default content before executing this javascript resolved the error.

Hope it'll help if the problem is still valid for you.

In one word, just before calling WaitForPageToLoad() I've used _driver.SwitchTo().DefaultContent();