Search code examples
c#seleniumactionmouseovermousehover

C# Selenium - Unable to get background color of element mouse is hovering over


I'm doing this in my method:

var firstCategoryTitle = pageTypeCategoryDiv.FindElement(By.ClassName("result.firstCategory")); // this is definitely selecting the correct element

Actions action = new Actions(Driver);
            action.MoveToElement(firstCategoryTitle).Click().Build().Perform();

            firstCategoryTitle.GetCssValue("background-color").Should().Be("rgba(0, 155, 212, 1)");

For some reason, even though the row element is being highlighted when the mouse clicks it, the background color changes to blue (as expected) but once the test finishes running it throws an error because it can't find the expected color (the expected blue) and finds white instead consistantly even though the element will be highlighted blue.

Any help's appreciated.


Solution

  • Add a delay, just to check if GetCssValue statement is not executed before the actual color change.

            Actions action = new Actions(Driver);
            action.MoveToElement(firstCategoryTitle).Click().Build().Perform();
            Thread.Sleep(5000);
            firstCategoryTitle.GetCssValue("background-color").Should().Be("rgba(0, 155, 212, 1)");