0

Selenium WebDriver in Visual Studio With C#. I've create a driver Object, which I'm using an instance of.

I'm trying to Access the top menu on this site: http://store.demoqa.com/

The second menu element, "Product Category", has a submenu. I'm trying to emulate a mouse-over of the "Product Category" element, then select and click the first element in the list (Accessories).

This is what I've got so far, pieced together by several searches here and elsewhere. It works up the point of clicking on the menu item ("Accessories"). I see that the top element is selected, and that the menu item is "selected" because it slightly indents when hovering the mouse over it. However, from there I cannot seem to Click() it.

"menu-item-33" is the top menu item, Product Category. "menu-item-34" is the sub menu item Accessories.

Actions action = new Actions(FFDriver.Instance);
IWebElement we = FFDriver.Instance.FindElement(By.Id("menu-item-33"));
action.MoveToElement(we).MoveToElement(FFDriver.Instance.FindElement(By.Id("menu-item-34"))).Click().Build().Perform(); 

It moves to the correct item, but the Click() function doesn't seem to work, since the page isn't changed.

Pardon me if this is too little information, but I've tried to keep it narrowed down to the code that seems to be the struggle.

1 Answer 1

1

I do not see why the code you have wouldn't work. However, you can try implementing some explicit wait if necessary. I have tried the following and it works. Note: I always suggest you to use id for locating element. But, I thought I show you another option and directly finding the anchor will be wiser for submenu item

By byId = By.Id("menu-item-33");
By css = By.CssSelector("a[href*='product-category/accessories']");

Actions action = new Actions(_driver);
IWebElement we = _driver.FindElement(byId);
action.MoveToElement(we).Build().Perform();
new WebDriverWait(_driver,TimeSpan.FromSeconds(2)).Until(ExpectedConditions.ElementIsVisible(css)).Click();
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, that worked. I thought about trying to find the elements in different ways from ID, but I couldn't see why that would make a difference. So why does this work, but the other not?
And if I use your code and replace the By.CssSelector with By.Id("menu-item-34"), it doesn't work. It doesn't even seem to find the menu item. But that's maybe because finding by Id is different than by CssSelector?
Well, with By.Id("menu-item-34") you are finding the li not anchor. Anchor(a tag) is more precise. You can also try your code with By css = By.CssSelector("a[href*='product-category/accessories']"); and that should work too.
And, I do have some explicit wait that makes sure the element is ready for some interaction whereas you do not
True, about the waits. However, I didn't add them in my code because I tried adding them and it didn't help. But I agree that wait(s) is a good idea when working With elements like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.