【问题标题】:Hover -> wait for visibility -> hover again (no sleeps)悬停 -> 等待可见性 -> 再次悬停(不休眠)
【发布时间】:2014-10-03 00:10:22
【问题描述】:

我有一个类似菜单的结构,将鼠标悬停在菜单项上会在一些延迟后打开一个子菜单。我在使用 Selenium 的确定性方法(= 不使用 Thread.Sleep)对此菜单进行网络测试时遇到问题。

  • 要将鼠标悬停在元素上,我需要使用 Selenium 的 Actions 构建器类 (MoveToElement)
  • 要等待子菜单可见,我需要使用WebDriverWait.Until(d => subMenuWebElement.Displayed)

如何结合这两种方法?我还没有找到将 WebDriverWait.Until 调用添加到 Actions 对象的方法。解决我的问题的推荐方法是什么?

我在 SOF 上找到了各种其他线程,但是,它们要么只解决了上述两个要点之一,要么没有有效的答案(例如 Selenium WebDriver MoveToElement - hidden element, hover and toggleClass)。

希望有人可以提供帮助:-)

【问题讨论】:

    标签: c# selenium selenium-webdriver


    【解决方案1】:

    您可以拆分操作生成器。无需一次性完成。

    构建并执行悬停动作。使用 webdriver 等待。然后构建并执行一个新动作

    【讨论】:

    • 我看不出这应该如何工作。我读到,为了让鼠标在一个元素上悬停更长时间,所有操作都必须捆绑到一个 Actions 构建器中。
    • 无论你在哪里读到,都是错误的。我的操作需要鼠标悬停等待 0.15 直到应用程序注册它。在这里,我有一个用于鼠标悬停的构建器,然后执行,然后是睡眠,然后是另一个构建器,用于后续操作。您是否尝试过分离您的构建器?
    【解决方案2】:

    尝试让 JavascriptExecutor 参与进来

    第 1 步

    String menu_selector="abracadabra";
    
    ((JavascriptExecutor)driver).executeScript("$(\'"+menu_selector+"\').hover();");
    

    注意:如果支持 jQuery,这对你有用。

    第 2 步 然后涉及到fluentWait等待子菜单出现:

    String subMenu_selector="blablabla";
    
    
    fluentWait(By.cssSelector(subMenu_selector));
    
    
       public WebElement fluentWait(final By locator) {
    /*
    fluent wait impementation documentation:
            http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html
    */
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    
                    .withTimeout(10, TimeUnit.SECONDS)
                    .pollingEvery(10, TimeUnit.MILLISECONDS)
    
    //                .ignoring(NoSuchElementException.class);
                    .ignoring(org.openqa.selenium.NoSuchElementException.class);
    
            WebElement foo = wait.until(
                    new Function<WebDriver, WebElement>() {
                        public WebElement apply(WebDriver driver) {
                            return driver.findElement(locator);
                        }
                    }
            );
            return foo;
        }
    
        ;
    

    【讨论】:

    • 如果尝试测试网站,应避免使用 JavaScript。用户无法直接触发 JavaScript,因此测试并未验证用户是否可以与 AUT 交互。作为最后的手段,您应该尝试使用 Selenium 交互和回退 JavaScript 执行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2015-10-18
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多