如何使用Selenium Webdriver等待元素不可单击? [英] How to wait for an element NOT to be clickable using Selenium Webdriver?

查看:370
本文介绍了如何使用Selenium Webdriver等待元素不可单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写测试以验证某个元素在某些情况下是否不可单击.我正在使用Selenium Webdriver Java库.

I have to write a test to verify if an element is NOT clickable under certain circumstances. I'm using the Selenium Webdriver Java library.

我知道如何利用WebDriverWaitExpectedConditions类来做相反的事情.

I know how to do the opposite thing by utilizing the WebDriverWait and ExpectedConditions classes.

 new WebDriverWait(element, TIMEOUT).until(ExpectedConditions
     .elementToBeClickable(By.cssSelector(LOCATOR)));

但是我似乎找不到一种方法来检查相反的事情.

but I can't seem to find a way to check the opposite thing.

我已经阅读了WebElement的文档,并设法将问题分解为两个更简单的案例.

I've gone through the docs for WebElement and managed to break down the problem into two simpler cases.

  1. 该元素被禁用-在这种情况下,我可以只使用WebElement#isEnabled并将其包装在传递给WebDriverWait#until的谓词中.

  1. The element is disabled - in this case I can just use WebElement#isEnabled and wrap it in a predicate passed to WebDriverWait#until.

new WebDriverWait(driver, TIMEOUT).until(new Predicate<WebDriver>(){
    public boolean apply(WebDriver driver) {
        return !getElement().isEnabled();
    }
});  

  • 在某些情况下,元素在单击时不执行任何操作.我也要考虑到这一点.问题是,该元素本身并未被禁用.可以单击它,但没有注册任何事件(通常有一个事件,但就我而言,它已被JavaScript删除).

  • In some cases, the element does not do anything on click. I want to take this into account as well. The problem is, the element is not disabled per se. It can be clicked but there is no event registered (usually, there is one but in my case, it's being removed by JavaScript).

    第二种情况仍然困扰着我.我有什么办法可以等待此类事件被取消注册?

    The second case still troubles me. Is there any way I can wait for such an event to be unregistered?

    推荐答案

    您的案例1的解决方案似乎应该可以工作.

    Your solution for case 1 seems like it should work.

    对于情况2,您可能需要从Selenium内部调用页面上的JavaScript,以检查该元素是否分配了任何事件处理程序,我相信以下方法可以解决问题:

    For case 2, you'll probably need to invoke JavaScript on the page from within Selenium to check if the element has any event handlers assigned, I believe the following should do the trick:

    new WebDriverWait(driver, TIMEOUT).until(new Predicate<WebDriver>() {
        public boolean apply(WebDriver driver) {
            return (JavascriptExectutor) driver.executeScript(
                        "arguments[0].onclick == null;", getElement());
        }
    }); 
    

    这应该使JS查询从方法getElement()传递给JavascriptExecutor的元素的当前onClick事件处理程序,如果未分配处理程序,则返回true.

    This should get JS to query the current onClick event handler assigned to the element passed into the JavascriptExecutor from the method getElement() and return true if no handler is assigned.

    除了使用JS进行快速摆弄外,还没有进行实际测试,但这应该很好.

    Not actually tested this beyond a quick fiddle with the JS, but it should be all good.

    更新: 在较新的硒版本中,您应该可以使用:

    Update: In newer selenium versions, you should be able to use:

    ExpectedConditions.not(ExpectedConditions.elementToBeClickab‌​le(By))
    

    这篇关于如何使用Selenium Webdriver等待元素不可单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆