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

Discussão em 'Angular' iniciado por QA Testing, Março 11, 2025.

  1. QA Testing

    QA Testing Guest

    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. 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.com/questions/72938900/getting-this-error-org-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.XPath, Using = "//*[@id='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?

    Continue reading...

Compartilhe esta Página