简体   繁体   English

Selenium WebDriver SubMenu点击不起作用

[英]Selenium WebDriver SubMenu click not working

I am not able to Click on SubMenu item using selenium webdriver using c#. 我无法使用c#使用selenium webdriver单击SubMenu项目。 I am using IE9 and FireFox 13. I have tried Action Builder but it does not work. 我正在使用IE9和FireFox 13.我尝试过Action Builder但它不起作用。 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();"); 我甚至使用过javascript:js.ExecuteScript(“return $(\\”a:contains('ID1')\\“)。mouseover();”); // Mouse hove to main menu //鼠标移动到主菜单
webDriver.FindElement(By.Id("ID2")).Click(); 。webDriver.FindElement(By.Id( “ID2”))点击();

Please give some solution to click on hidden elements 请给出一些点击隐藏元素的解决方案

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.). 您可以使用预期条件等待元素在其上方悬停后可点击( Thread.sleep()几乎总是不好的选择。并且5毫秒是不够的。)。

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. 文档此类( ExpectedConditionsOpenQA.Selenium.Support.UI命名空间)被打破,因为我现在可以看见他们,但如果你能按照上面的链接的Java代码, 这里是Java的预期条件-这是真的差点同样在C#中也是如此。

Instead of using the statement Thread.sleep() . 而不是使用语句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. 获得要单击的WebElement后,使用ExpectedContition语句中的isDisplayed()方法检查是否显示了该元素,以及@Slanec在上面的帖子中讨论的内容。

By this you can make sure you will click on the element only after Wait.Until() returns true . 通过这个,您可以确保只有在Wait.Until()返回true后才会单击该元素。 ie the menuOption is displayed. 即显示menuOption

I'm writing the code in java as I do not know C#. 我正在用java编写代码,因为我不知道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. 我希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM