I have a element I am trying to click that is part of a dropdown in a mobile hamburger menu. This dropdown only has two elements/ options in it. I am trying to click the second element. However, when the test is running the test runs too fast, causing the other element in the dropdown to be clicked first. Even though my XPath explicitly calls out the second element I actually want to click. This test is being run in an iOS Mobile emulation chrome window.
What I believe the problem to be is that the dropdown has a nice/ smooth slide effect, making it so that when the dropdown is opened the two elements are kind of on top of each other as they are sliding into place. So the WebDriver tries to click on the second element as it should, but it's sort of 'behind' the first element in a way. The WebDriver ends up actually clicking the first element. I hope this makes sense.
So has any one come across an issue like this, have any slick tricks to help wait for the dropdown to be fully open before selecting a menu item?
The elements are setup like this
<ul class="hamburger-menu">
<li>Menu Item</li>
<li>Dropdown Menu
<div class="mobile-dropdown">
<div class="dropdown-element-1"></div>
<div class="dropdown-element-2"></div>
</div>
</li>
<li>Menu Item</li>
</ul>
This is a rough version of what I am doing in the code
public static Main()
{
By hamburgerMenu = By.XPath("Not important, but the xpath to the menu here");
By hamburgerMenuButton = By.ClassName("hamburger-menu");
By dropdownMenuButton = By.XPath("//ul[@class='hamburger-menu']/li[2]");
By dropdownElement1 = By.XPath("//div[@class='dropdown-element-1']");
By dropdownElement2 = By.XPath("//div[@class='dropdown-element-2']");
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://sample.com");
// Click to open hamburger menu
driver.FindElement(hamburgerMenuButton).Click();
//Here I implement a selenium Explicit wait that waits for the hamburger menu to be fully open
_ = Extensions.WaitForVisible(driver.FindElement(hamburgerMenu), 10);
// Click to open dropdown menu
driver.FindElement(dropdownMenuButton).Click();
//Here I assert that the two elements are present
Assert.True(driver.FindElement(dropdownElement1).Displayed);
Assert.True(driver.FindElement(dropdownElement2).Displayed);
//I need some type of wait here?
//Then I click on the second element
driver.FindElement(dropdownElement2).Click();
}
I can't seem to get an Implicit or Explicit wait to work, because those elements are already available in the DOM, so the WebDriver picks them up instantly. There doesn't seem to be anything to 'wait' for. The elements don't have any attributes that make them 'visible' or anything once they are in place under the dropdown menu.