7

I have a number of tests. Sometimes if an element can't be found it just clicks on the top left of the screen. This doesn't happen all the time however it does happen. I'm not sure why this is happening. In my setUp method I'm telling it to click the element "Maximize" however if it can't find that element I put it into a catch and ignore it. For some reason when it can't find the element it just clicks on the top left corner of the screen that has the application session.

Has anyone any ideas why this is happening or is it just how selenium sometimes responds

My code is as follows

private string wordId = OfficeVersion.Word();
    private string excelId = OfficeVersion.Excel();
    private string powerPointId = OfficeVersion.PowerPoint();
    private const string AppDriverUrl = "http://127.0.0.1:4723";
    public static WindowsDriver<WindowsElement> excelSession;
    public static WebDriverWait webDriverWait;
    xl.Workbook WB;
    public static bool skipTearDown = false;
    WindowsElement create;
    WindowsElement blankWorkBook;
    public static DesiredCapabilities appCapabilities = new DesiredCapabilities();
    [TestInitialize]
appCapabilities.SetCapability("app", excelId);

            var initialSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);

            var capabilities = new DesiredCapabilities();
            capabilities.SetCapability("app", "Root");
            excelSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), capabilities);
            webDriverWait = new WebDriverWait(excelSession, TimeSpan.FromSeconds(10));

            CommonMethods.keyCheck(excelSession);
            webDriverWait = new WebDriverWait(excelSession, TimeSpan.FromSeconds(10));
            CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Create error when launching Excel");
            try
            {

This is the element I'm having trouble ignoring if it doesn't exist

                webDriverWait.Until(ExpectedConditions.ElementTo‌​BeClickable(excelSession.FindElementByName("Maximize"))).Click();
            }
            catch (Exception)
            {

                //ignore
            }
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • My memory of Selenium is hazy; but could it be that it's actually finding the element in the DOM, even though it's not on the page? After you find the element, you probably need to check whether the element is visible before trying to click on it. – jpaugh Aug 10 '17 at 18:16

2 Answers2

0

You can try to get the current window handle first and then attempt to locate and get the Webelement that points to the Maximize button for the window. Possibly, you might also need give a simple wait on the WebElement locate just to be safe.

This api might be useful for a C# client to selenium - driver.SwitchTo().Window(handle)

And for details, you can check here

0

I was running into the same problem when trying to select an item from a combo-box. Trying click on the item would always just result in a click on the top left of the screen. Super frustrating.

I got around it by using an Action to move the mouse to the element and then performing a click.

var a = new Actions(Session);
a.MoveToElement(v);
a.Click();
a.Perform();
Dan
  • 1,805
  • 2
  • 18
  • 21