question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FindElementByName - Element couldn't be located

See original GitHub issue

Hi,

I have a test which passes in Visual Studio debug mode but fails when it is run as normal. The test is:

AppSession.FindElementByAccessibilityId("UsernameTextBox").SendKeys("login");
            AppSession.FindElementByAccessibilityId("PasswordTextBox").SendKeys("password");
            
            var loginButton = AppSession.FindElementByAccessibilityId("LoginButton");
            loginButton.Click();

            AppSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(5000));
            var window = AppSession.FindElementByName(windowName);

            Assert.IsNotNull(window);

This code is run on test initialization:

private const string ServerURL = "http://127.0.0.1:4723";
        protected DesiredCapabilities AppCapabilities { get; set; }
        protected WindowsDriver<WindowsElement> AppSession { get; set; }

        protected void InitApplication()
        {
            AppCapabilities = new DesiredCapabilities();
            AppCapabilities.SetCapability("app", $"{Environment.CurrentDirectory}\\app.exe");
            AppSession = new WindowsDriver<WindowsElement>(new Uri(ServerURL), AppCapabilities);
        }

When I put breakpoint on the line var window = AppSession.FindElementByName(windowName); then test passes in debug mode. Otherwise it fails with exception System.InvalidOperationException: An element could not be located on the page using the given search parameters.

After login button is clicked the Window is maximized and it’s name gets updated.

Any help?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
brett-burkhartcommented, Mar 9, 2018

@lekso81 - here’s how I accomplished this in C# for a login workflow scenario. After login, the code waits for the main window using the DefaultWait which will try to find the element every 1 second until it finds it or until the timeout expires…

    loginWindow.FindElementByAccessibilityId("Submit").Click();

    var wait = new DefaultWait<WindowsDriver<WindowsElement>>(_session)
    {
        Timeout = TimeSpan.FromSeconds(60),
        PollingInterval = TimeSpan.FromSeconds(1)
    };
    wait.IgnoreExceptionTypes(typeof(InvalidOperationException));

    WindowsElement mainWindow = null;

    wait.Until(driver =>
    {
        driver.SwitchTo().Window(driver.WindowHandles[0]);

        mainWindow = driver.FindElementByAccessibilityId("MainWindow");

        return mainWindow != null;
    });

…not sure if this is best approach, but it works better than Thread.Sleep() IMHO since I cannot control how long it might actually take for the login process to complete and the main window to finally open.

1reaction
lekso81commented, Mar 9, 2018

Hi,

thank you for your responses. My tests now pass, but only a Thread.Sleep(5000) statement after Click event helps. The WaitForElementToExist function doesn’t work and always returns null. This is what I implemented using recommendation from PandaMagnus:

protected WindowsElement WaitForElementToExist(Func<string, WindowsElement> findElement, string elementName, int waitTime = 10)
        {
            WindowsElement result = null;
            var timer = new Stopwatch();
            timer.Start();
            do
            {
                try
                {
                    WindowsElement elem = findElement(elementName);
                    if (elem != null)
                    {
                        result = elem;
                        break;
                    }
                }
                catch { }

                Thread.Sleep(100);
            } while (timer.Elapsed < TimeSpan.FromSeconds(waitTime));

            return result;
        }

It is used as:

var window = this.WaitForElementToExist(AppSession.FindElementByName, windowName);
Assert.IsNotNull(window);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Element not Interactable when using the selenium function ...
I have webpage where I am trying to login to it. When I use find element by name method to find the elements...
Read more >
Selenium can't locate element by xpath
To solve this you should wait till the webelement gets loaded on to the webpage. One way to do this to use WebDriverWait...
Read more >
F5 LTM SOAP API - Ruby f5-icontrol Gem - Could not...
Regarding the "couldn't find element by name" error, that means that the server didn't see the specific parameter in the request. In your...
Read more >
Unable to locate an element using xpath error in selenium- ...
We may encounter the error - unable to locate element while working with Selenium webdriver. This leads to NoSuchElementException.
Read more >
Problem with find Element - Issues/Bugs
I want to test it on a List, the elements i want to find are visible. ... {“status”:7,“value”:{“message”:“An element could not be located...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found