最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C# Selenium "window.angular is undefined" when waiting for elements to be displayed - Stack Overflow

programmeradmin4浏览0评论

I have a C# automation framework with Selenium, Webdriver and XUnit. I have the following issue on my app, but I will give you an example using StackOverflow page, as I get the same issue.

I have a complex automation framework but I will try to explain it as much as I can.

I have a test where I'm navigating to a StackOverflow page, waiting for the element to be displayed and trying to click on the logo. The elements are displayed on the page, but I get the following error message:

    Message: 
  OpenQA.Selenium.JavaScriptException : javascript error: window.angular is undefined.  This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See  for details
  JavaScript stack:
  Error: window.angular is undefined.  This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See  for details
      at eval (eval at <anonymous> (:457:26), <anonymous>:79:11)
      at eval (eval at <anonymous> (:457:26), <anonymous>:91:4)
      at <anonymous>:457:47
      at new Promise (<anonymous>)
      at executeAsyncScript (<anonymous>:451:17)
      at <anonymous>:481:29
      at callFunction (<anonymous>:386:22)
      at <anonymous>:400:23
      at <anonymous>:401:3
    (Session info: chrome=134.0.6998.36)

Stack Trace: 
  WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute)
  WebDriver.ExecuteAsync(String driverCommandToExecute, Dictionary`2 parameters)
  WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
  WebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
  WebDriver.ExecuteAsyncScript(String script, Object[] args)
  NgWebDriver.ExecuteAsyncScript(String script, Object[] args)
  NgWebDriver.WaitForAngular()
  NgWebDriver.FindElement(By by)
  ISearchContext.FindElement(By by)
  DefaultElementLocator.LocateElement(IEnumerable`1 bys)
  WebElementProxy.get_WrappedElement()
  WebElementProxy.get_Displayed()
  <>c__DisplayClass0_0.<ElementToBeDisplayed>b__0(IWebDriver driver) line 10
  DefaultWait`1.Until[TResult](Func`2 condition, CancellationToken token)
  DefaultWait`1.Until[TResult](Func`2 condition)
  BaseComponent.WaitForElementToBeDisplayed() line 227
  LogInLogOutValidation.Test() line 40
  RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
  MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

The test looks like this:

[Fact]
public void Test()
{
    DriverFactory.CurrentDriver.SetUrl(";);

    _stackOverflow.ButtonLogo.WaitForElementToBeDisplayed();
    _stackOverflow.ButtonLogo.Click();

    var title = _stackOverflow.LabelMainBar.GetText();

    Assert.Equal("Newest Questions", title);
}

ButtonLogo selector:

[FindsBy(How = How.ClassName, Using = "s-topbar--logo")]
private IWebElement _buttonLogo;

Where the methods like WaitForElementToBeDisplayed() or Click() are using FluentWait approach:

    public void WaitForElementToBeDisplayed()
    {
        GetFluentWait().Until(CustomExpectedConditions.ElementToBeDisplayed(_baseWebElement));
    }

protected DefaultWait<IWebDriver> GetFluentWait()
{
    WebDriverWait wait = new WebDriverWait(DriverFactory.CurrentDriver, TimeSpan.FromSeconds(10))
    {
        PollingInterval = TimeSpan.FromSeconds(1)
    };

    wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException), typeof(StaleElementReferenceException));

    return wait;
}

What I've observed is that even if I'm doing a retry after every 1 second for max 10 seconds, the test is passing fast over this FluentWait and I can't understand why.

I have the same issue in Debug mode, even if I wait to see the element on the page.

I have also tried Thread.Sleep(7000) before interacting with the element, but I have the same issue.

Does anyone know how to wait for the ButtonLogo so I can click on it?

I have a C# automation framework with Selenium, Webdriver and XUnit. I have the following issue on my app, but I will give you an example using StackOverflow page, as I get the same issue.

I have a complex automation framework but I will try to explain it as much as I can.

I have a test where I'm navigating to a StackOverflow page, waiting for the element to be displayed and trying to click on the logo. The elements are displayed on the page, but I get the following error message:

    Message: 
  OpenQA.Selenium.JavaScriptException : javascript error: window.angular is undefined.  This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details
  JavaScript stack:
  Error: window.angular is undefined.  This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details
      at eval (eval at <anonymous> (:457:26), <anonymous>:79:11)
      at eval (eval at <anonymous> (:457:26), <anonymous>:91:4)
      at <anonymous>:457:47
      at new Promise (<anonymous>)
      at executeAsyncScript (<anonymous>:451:17)
      at <anonymous>:481:29
      at callFunction (<anonymous>:386:22)
      at <anonymous>:400:23
      at <anonymous>:401:3
    (Session info: chrome=134.0.6998.36)

Stack Trace: 
  WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute)
  WebDriver.ExecuteAsync(String driverCommandToExecute, Dictionary`2 parameters)
  WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
  WebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
  WebDriver.ExecuteAsyncScript(String script, Object[] args)
  NgWebDriver.ExecuteAsyncScript(String script, Object[] args)
  NgWebDriver.WaitForAngular()
  NgWebDriver.FindElement(By by)
  ISearchContext.FindElement(By by)
  DefaultElementLocator.LocateElement(IEnumerable`1 bys)
  WebElementProxy.get_WrappedElement()
  WebElementProxy.get_Displayed()
  <>c__DisplayClass0_0.<ElementToBeDisplayed>b__0(IWebDriver driver) line 10
  DefaultWait`1.Until[TResult](Func`2 condition, CancellationToken token)
  DefaultWait`1.Until[TResult](Func`2 condition)
  BaseComponent.WaitForElementToBeDisplayed() line 227
  LogInLogOutValidation.Test() line 40
  RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
  MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

The test looks like this:

[Fact]
public void Test()
{
    DriverFactory.CurrentDriver.SetUrl("https://stackoverflow/questions/72938900/getting-this-error--openqa-selenium-javascriptexception-javascript-error-a");

    _stackOverflow.ButtonLogo.WaitForElementToBeDisplayed();
    _stackOverflow.ButtonLogo.Click();

    var title = _stackOverflow.LabelMainBar.GetText();

    Assert.Equal("Newest Questions", title);
}

ButtonLogo selector:

[FindsBy(How = How.ClassName, Using = "s-topbar--logo")]
private IWebElement _buttonLogo;

Where the methods like WaitForElementToBeDisplayed() or Click() are using FluentWait approach:

    public void WaitForElementToBeDisplayed()
    {
        GetFluentWait().Until(CustomExpectedConditions.ElementToBeDisplayed(_baseWebElement));
    }

protected DefaultWait<IWebDriver> GetFluentWait()
{
    WebDriverWait wait = new WebDriverWait(DriverFactory.CurrentDriver, TimeSpan.FromSeconds(10))
    {
        PollingInterval = TimeSpan.FromSeconds(1)
    };

    wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException), typeof(StaleElementReferenceException));

    return wait;
}

What I've observed is that even if I'm doing a retry after every 1 second for max 10 seconds, the test is passing fast over this FluentWait and I can't understand why.

I have the same issue in Debug mode, even if I wait to see the element on the page.

I have also tried Thread.Sleep(7000) before interacting with the element, but I have the same issue.

Does anyone know how to wait for the ButtonLogo so I can click on it?

Share Improve this question edited Mar 11 at 12:47 QA Testing asked Mar 11 at 8:57 QA TestingQA Testing 335 bronze badges 3
  • Why are you using CustomExpectedConditions when there's a supplied ExpectedConditions that has been tested by millions and used for years? You need to post the code for .ElementToBeDisplayed(). Polling at 1s is going to significantly slow down your scripts. – JeffC Commented Mar 11 at 14:21
  • looks like you are using "NgWebDriver" here and it's throwing because it's not on an Angular site, or is on an Angular site past v12. This library is probably not needed at all. I'd axe it and use proper webdriverwaits with explicit conditions. I'd also avoid using "FindsBy" as it's likely to lack a proper wait. (use implicits across the board if you must... but better to ditch those annotations and use explicit waits) – browsermator Commented Mar 11 at 21:35
  • Can you post the full code for the _stackOverflow field? – Greg Burghardt Commented Mar 12 at 13:18
Add a comment  | 

1 Answer 1

Reset to default 2

This simple code works just fine. My assumption is that somewhere in your "complex automation framework" is a bug or bad design that is causing the issue.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;

IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
string url = "https://stackoverflow/questions/72938900/getting-this-error--openqa-selenium-javascriptexception-javascript-error-a";

driver.Url = url;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector(".s-topbar--logo"))).Click();

string title = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("h1.flex--item"))).Text;
Assert.AreEqual("Newest Questions", title);

driver.Quit();
发布评论

评论列表(0)

  1. 暂无评论