Problem Statement
I am using the Selenium WebDriver to clickon a menu item which is not visible until you hover over a toplevel menu, and when I invoke the Click method on the IWebElement,an ElementNotVisibleExcepti
The problem is that I have made the IWebElement visible by hoveringover the top level menu.
As an aside, here is the code I used to hover over the topmenu:
1: var womensShoesMenu = this.webDriver.FindElement(By.CssSelector("a.topNavLink[href='/Womens-Shoe-Store']"));
2: var builder = new Actions(this.webDriver);
3: builder.MoveToElement(womensShoesMenu).Build().Perform();
4:
This is the code I used to invoke the Click method:
1: var wait = new WebDriverWait(this.webDriver, TimeSpan.FromSeconds(5));
2: var womensBoots = wait.Until((p) => this.webDriver.FindElement(By.CssSelector("#topNavigation ul div.menuItem a[href='/Womens-Boots']")));
3: womensBoots.Click();
4:
Solution
Instead of calling the Click method on the Selenium IWebElementobject, I used JavaScript instead.
1: var womensBoots = this.webDriver.FindElement(By.CssSelector("#topNavigation ul div.menuItem a[href='/Womens-Boots']"));
2: ((IJavaScriptExecutor)this.webDriver).ExecuteScript("arguments[0].click();", womensBoots);
3:
Updated Solution
I’m adding a second solution after posting thisarticle.
private static void DoWait(int milliseconds)
{
var wait = new WebDriverWait(webDriver, TimeSpan.FromMilliseconds(milliseconds));
var waitComplete = wait.Until<<span style="color: rgb(0, 0, 255);">bool>(
arg =>
{
System.Threading.Thread.Sleep(milliseconds);
return true;
});
}
var womensBoots = webDriver.FindElement(By.CssSelector("#topNavigation ul div.menuItem a[href='/Womens-Boots']"));
DoWait(1000);
womensBoots.Click();
((IJavaScriptExecutor)webDriver).ExecuteScript("arguments[0].click();", womensBoots);
Discussion
I’m new to Selenium, only having started using it as a developer afew days ago, so it is very likely I’m simply doing this wrong andI honestly have no idea of the problem I’m encountering is a bug ornot.