I have been messing arround lately with selenium in c#. I have stumbled upon a problem which I am currently not able to resolve. Basically, I am trying to open a hamburger "menu" on the top-left corner of the website and hover over to its first menu item: "Loterie" which should trigger another submenu to pop up right next to the hamburger menu with some "sub-options" to "Loterie".
Scenario would be: Menu -> Loterie -> User is able to see these options: Sportka / Eurojackpot / Rychle Kacky / etc....
First, I used Thread.Sleep to accomplish my task and it worked, as shown here:
m_driver = new ChromeDriver("C:/Users/richard/source/repos/ConsoleApp43");
m_driver.Url = "https://www.sazka.cz/";
m_driver.Manage().Window.Maximize();
IWebElement subMenu = m_driver.FindElement(By.Id("header-menu")); subMenu.Click();
IWebElement element = m_driver.FindElement(By.XPath("//*[@id='menuElem']/li[1]/a"));
Actions action = new Actions(m_driver);
System.Threading.Thread.Sleep(1000);
action.MoveToElement(element).Perform();
However, it was mentioned that this is a bad practice and that WebDriverWait class should be used instead. So I tried to use the WebDriverWait class but I am not able to get it running.
I have found couple of solutions which should be dealing with my problem, but none of them seem to be working, e.g.
Explicit waits in Selenium C# doesn't work . What is wrong?
Implicit wait Command Not Working-selenium webdriver C#
How to get webDriver to wait for page to load (C# Selenium project)
I have ended up with something like this. However, I am still not able to get it run, since with the following code only 50% of the tests runs are executed correctly for some reason.
m_driver = new ChromeDriver("C:/Users/richard/source/repos/ConsoleApp43");
m_driver.Url = "https://www.sazka.cz/";
m_driver.Manage().Window.Maximize();
IWebElement menu = m_driver.FindElement(By.Id("header-menu")); menu.Click();
WebDriverWait wait = new WebDriverWait(m_driver, TimeSpan.FromSeconds(1));
var loterie = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='menuElem']/li[1]/a")));
Actions action = new Actions(m_driver);
action.MoveToElement(loterie).Build().Perform();
Could somebody please help me?
EDIT: The main issue is that sometimes when I run the test the code works as it should (=hamburger menu rolls down and the mouse hovers over to "Loterie"). However in some other cases the hamburger menu only rolls down and the mouse does not hover over to "Loterie", so the user is left only with the opened hamburger menu and the "sub-menu" does not get triggered.