Skip to content

Azure pipeline giving error An element could not be located at line var sessionb = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions); #2051

Open

Description

@omkar4289

I am getting error:
ClassInitialize threw exception. OpenQA.Selenium.WebDriverException: OpenQA.Selenium.WebDriverException: An element could not be located on the page using the given search parameters..
2025-02-13T07:48:27.0449820Z Stack Trace:
2025-02-13T07:48:27.0450277Z at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
2025-02-13T07:48:27.0450578Z at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) 2025-02-13T07:48:27.0451173Z at OpenQA.Selenium.Appium.AppiumDriver1.Execute(String driverCommandToExecute, Dictionary2 parameters) 2025-02-13T07:48:27.0452283Z at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) 2025-02-13T07:48:27.0452973Z at OpenQA.Selenium.Appium.AppiumDriver1.FindElementByName(String name)

on line:
var sessionb = new WindowsDriver(new Uri("http://127.0.0.1:4723"), appiumOptions);

in my local machine it's working fine but I am getting this error when I run it through azure pipeline.

It is a desktop application and I have used winappdriver to automate it.
Below tasks I have used for release pipeline but not sure whether it is opening the application or not

Image

Activity

  1. liljohnak commented on Feb 13, 2025

    @liljohnak

    omkar4289 From your stack, looks like the Azure environment is slower than locally. Put a delay until the element with that name exists.
    Check exists by a loop using FindElements instead of FindElement.

  2. omkar4289 commented on Feb 14, 2025

    @omkar4289
    Author

    Hi Jonathan Neal (@liljohnak) ,
    Thank you for your response.

    Now I am getting below error:
    System.NullReferenceException: Object reference not set to an instance of an object.

    but same works fine when I run the code locally in my machine

  3. Shakevg commented on Feb 14, 2025

    @Shakevg

    omkar4289 Please provide code example, try to use waiters

  4. omkar4289 commented on Feb 14, 2025

    @omkar4289
    Author

    Jonathan Neal (@liljohnak) Shakevg
    PFB:
    public class SessionInit
    {
    protected static WindowsDriver sessionb;
    protected static WindowsDriver session;

        public static void StartSession(TestContext context)
        {
                string username = Environment.UserName;
                var appiumOptions = new OpenQA.Selenium.Appium.AppiumOptions();
                appiumOptions.AddAdditionalCapability("platformName", "Windows");
                appiumOptions.AddAdditionalCapability("app", "Root");
                appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
    
                
                 Thread.Sleep(10000);
                var sessionb = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions);
               
    
                // Attaching to existing Application Window
                
                var applicationwindow = sessionb.FindElementsByName("Hermes 5 (DEV) - HERMES_5_1_66_8.ECF 07/02/2025 19:07 (LIS_W1_66,PT_SHARE_66,ADMIN,ADMIN)");
                
                 
                 foreach (WindowsElement element in applicationwindow)
           {
                 if (element.Text.Contains("Hermes 5 (DEV)"))
               {
                   var topLevelWindowHandle = element.GetAttribute("NativeWindowHandle");
                   topLevelWindowHandle = int.Parse(topLevelWindowHandle).ToString("X");
    
                    var appopt = new OpenQA.Selenium.Appium.AppiumOptions();
                    appopt.AddAdditionalCapability("appTopLevelWindow", topLevelWindowHandle);
                   session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appopt);
                   
                }
            }
            
                try { session.Manage().Window.Maximize(); } catch { }
    

    }

        public static void EndSession()
        {
            // Close the application and delete the session
            if (session != null)
            {
                session.Close();
                session.Quit();
                session = null;
            }
        }
    }
    

    and I have test methods in below class
    [TestClass]
    public class OP_Exp_NOT_Out4 : SessionInit
    {
    [TestMethod]
    public void Test()
    {
    string Scriptid;
    Scriptid = "Exp_NTM_Out4";
    //////create 1st Export flight
    Thread.Sleep(1000);
    session.FindElementByAccessibilityId("MDIClientForm").Click(); // to click in background
    session.FindElementByAccessibilityId("2").FindElementByAccessibilityId("6").Click();
    Fun_exp_createnewflight.execute(Scriptid); //Create new Exp flight
    Thread.Sleep(2000);
    }

     [ClassInitialize]
     public static void ClassInitialize(TestContext context)
     {
         StartSession(context);
     }
    

    }

    I am getting error at line:
    session.FindElementByAccessibilityId("MDIClientForm").Click(); // to click in background
    System.NullReferenceException: Object reference not set to an instance of an object.
    may be because of session

  5. liljohnak commented on Feb 14, 2025

    @liljohnak

    omkar4289 for this one, all of the .FindElement() have an additional condition of being focusable and clickable. To relax these conditions use one of the .FindElements() methods and get the BoundingBox attribute. Then you can click the center of the bounding box.

    Another possibility is you need a delay for the button to become visible.

  6. omkar4289 commented on Feb 17, 2025

    @omkar4289
    Author

    Jonathan Neal (@liljohnak)
    still its giving the same error.
    System.NullReferenceException: Object reference not set to an instance of an object.

  7. liljohnak commented on Feb 17, 2025

    @liljohnak

    Maybe a 500 error on WinAppDriver.exe?

  8. anunay1 commented on Mar 4, 2025

    @anunay1

    Please check if winappdriver task has started successfully. Is it running with self hosted agent or Microsoft hosted agent? Also share the pipeline logs? I see that you are using powershell to run winappdriver.exe. Please introduce a sleep for 5 second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions