簡體   English   中英

Selenium C#等待頁面加載-UnexpectedJavaScriptError

[英]Selenium C# waiting for page to load - UnexpectedJavaScriptError

我正在使用selenium,IEDriver和C#,我想等待頁面加載。 我有以下代碼:

/// <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;
  }
}

但這會導致崩潰:

    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

在大多數情況下都可以使用,但是有時會在此崩潰

下一個方法是這樣的:

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();
    }

傳遞的webElement由XPath創建(webElement始終正確)

您可以嘗試調用WebDriverWait類。 您可以使用它來等待元素顯示在頁面上(即,如果存在此對象,則頁面加載)

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;
    }
}

在這段代碼中,該方法返回wait.until,直到element在存在於頁面上的元素上存在條件。 只是讓它嘗試在頁面加載時查找頁面上應該存在的元素。 如果在一定時間內不存在,它將給出NoSuchElementException(因此超時)。 抓住並給出自己的輸出

來源: 如何讓webDriver等待頁面加載(C#Selenium項目)

希望這對你有幫助

我遇到了同樣的問題,問題是我在框架內。 執行此javascript之前切換回默認內容可解決該錯誤。

希望該問題對您仍然有效。

一言以蔽之,在調用WaitForPageToLoad()之前,我已經使用_driver.SwitchTo().DefaultContent();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM