2

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.

apploid
  • 29
  • 3
  • Do you get any error when it fails? – Guy Oct 22 '18 at 13:26
  • How would I find out? I have looked into VS log and in VS it seems like everything is executing alright. In Chrome Dev tools I am getting this: `Uncaught ReferenceError: SCEChatOptions is not defined at Object.storeEvents` – apploid Oct 22 '18 at 13:54
  • Which lines causes a problem ? Is the problem wait timeout or click/hover ? I personally use driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(seconds); but until and common methods could be not working as desired sometimes, i ended with task.delay() when using async method – apincik Oct 22 '18 at 14:09
  • The problem is that in some cases, the mouse does not hover to "Loterie" and the hamburger menu rolls down only (as if the Expectedcondition would be ignored). I have tried using `m_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10)`, but still not working – apploid Oct 22 '18 at 14:18

1 Answers1

0

To invoke the url https://www.sazka.cz/, click on the Burger, Mouse Hover over Loterie and click() on the element with text as Sportka you can use the following solution:

m_driver = new ChromeDriver("C:/Users/richard/source/repos/ConsoleApp43");
m_driver.Url = "https://www.sazka.cz/";
m_driver.Manage().Window.Maximize();     
new WebDriverWait(m_driver, TimeSpan.FromSeconds(20)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='show-sidebar' and @id='header-menu']"))).Click();
var loterie = new WebDriverWait(m_driver, TimeSpan.FromSeconds(20)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//div[@class='menu-box open']//a[contains(@href,'loterie')]")));
new Actions(m_driver).MoveToElement(loterie).Build().Perform();
var sportka = new WebDriverWait(m_driver, TimeSpan.FromSeconds(20)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='menu-box open']//a[contains(@href,'loterie')]//following::ul[1]/li/a[contains(.,'Sportka')]")));
new Actions(m_driver).MoveToElement(sportka).Click().Build().Perform();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you, unfortunately, this is not working. I am getting the following error in VS: OpenQA.Selenium.WebDriverException : unknown error: Element is not clickable at point (-110, 95) and it does not hover over to "Loterie". – apploid Oct 22 '18 at 13:59
  • Okay, so the hamburger menu rolls down everytime now. The mouse also did hover to "Loterie" the first two times but I tried to execute it couple of more times and after the first two attempts the hamburger menu only rolled down and the mouse did not hover to "Loterie". – apploid Oct 22 '18 at 14:50
  • I am not sure where you are stuck, but the _Java_ and _Python_ version of the same code is executing just perfecto at my end. Are you seeing any error? Update the question please. – undetected Selenium Oct 22 '18 at 14:54
  • I am stuck at the "hover over to Loterie", since in some cases (test runs) only the hamburger menu rolls down but nothing happens after that. VS does not show any errors unfortunately and it seems like the test is running smoothly. – apploid Oct 22 '18 at 15:24
  • 1
    Adding ImplicitWait to DebanjanB's code works perfectly from my side. I just tested and it opens sportka page. – TwinckleTwinckle Oct 22 '18 at 18:14
  • 1
    @TwinckleTwinckle The _Java_ and _Python_ version of this code block works perfecto at my end. But we should avoid mixing up _ImplicitWait_ and _ExplicitWait_ because as per the [documentation](https://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits) _Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds_ – undetected Selenium Oct 22 '18 at 18:22
  • I agree with you. I used C sharp binding. Without implicitwait, it's not working. Anyways, thanks for the information. – TwinckleTwinckle Oct 22 '18 at 18:35