Pat
Pat

Reputation: 727

Selenium WebDriver SubMenu click not working

I am not able to Click on SubMenu item using selenium webdriver using c#. I am using IE9 and FireFox 13. I have tried Action Builder but it does not work. It gives an error saying element cannot be clicked.

    WebDriverWait Wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
    IWebElement menu = Wait.Until((d) => webDriver.FindElement(By.Id("id1")));
    IWebElement menuOption = Wait.Until((d)=>webDriver.FindElement(By.Id("ID2")));
            Actions builder = new Actions(webDriver);
            builder.MoveToElement(menu).Build().Perform();
            Thread.Sleep(5);
            //then click when menu option is visible
            menuOption.Click();

I have used even javascript : js.ExecuteScript("return $(\"a:contains('ID1')\").mouseover();"); // Mouse hove to main menu
webDriver.FindElement(By.Id("ID2")).Click();

Please give some solution to click on hidden elements

Upvotes: 1

Views: 2717

Answers (2)

Hari Reddy
Hari Reddy

Reputation: 3858

Instead of using the statement Thread.sleep(). You can try to click on the element after making sure that it is displayed.

After you get the WebElement you want to click on , check if it is displayed by using the isDisplayed() method within the ExpectedContition statement about which @Slanec is talking about in the above post.

By this you can make sure you will click on the element only after Wait.Until() returns true. i.e the menuOption is displayed.

I'm writing the code in java as I do not know C#. But I guess you can figure out what I'm trying to say -

    new WebDriverWait(driver, 60).until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver ) {


            return driver.findElement(By.Id("ID2")).isDisplayed();
        }
    });

I hope that this helps you.

Upvotes: 1

Petr Janeček
Petr Janeček

Reputation: 38444

You could use Expected Conditions to wait for element being clickable after hovering above it (Thread.sleep() is almost always the bad choice. And 5 ms won't be enough.).

The docs for this class (ExpectedConditions in OpenQA.Selenium.Support.UI namespace) are broken as I can see them now, but if you can follow the Java code in the link above, here are the Expected conditions for Java - it's really almost the same in C#, too.

Upvotes: 1

Related Questions