Search code examples
c#seleniumwebdriverscreenshotmouseover

Mouse interactions not showing in webdriver screenshots


I'm currently using Selenium WebDriver 2.35 and hit a roadblock when it comes to taking a screenshot. I wrote up a small function that takes in an IWebElement and will return a screenshot of the specific element. The element I am trying to screenshot is actually an image pulled from a sprite. This element is tricky though, because upon mouseover/hover the image changes from gray to its true color(by moving to a different part of sprite). I am able to get a proper screenshot of the image via this function, but cannot get it to recognize the mouse interactions with ITakesScreenshot. I can visually see in the browser that the image is hovered over, but the screenshot cannot. Any thoughts?

    public static Bitmap GetImage(IWebElement element)
    {
        RemoteWebDriver driver = BrowserManager.GetInstance().GetDriver();
        Actions action = new Actions(driver);

        //take screenshot of page
        action.MoveToElement(element).Build().Perform();
        Byte[] ba= ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
        Bitmap ss = new Bitmap(new MemoryStream(ba)); 
        //ss.Save("c:\\tmp\\ss.png", ImageFormat.Png);

        Rectangle crop = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);

        //create a new image by cropping the original screenshot
        Bitmap image = ss.Clone(crop, ss.PixelFormat);
        return image;
    }

Solution

  • It seems as though the newest selenium (2.39) has addressed this issue and I am able to see mouse hover in this screenshot method. Thanks everyone for your help!