Selenium clicking wrong element

Viewed 1137

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
            }
2 Answers

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();
Related