`elementToBeClickable`只检查它是否可见并启用

时间:2016-01-04 22:44:56

标签: java selenium selenium-webdriver

我是初学者,使用Selenium和Java编写测试,有时我需要点击的网页元素被其他人覆盖,现在根据this elementToBeClickable只检查它& #39; s可见并启用。有什么方法可以解决这个问题?

2 个答案:

答案 0 :(得分:2)

在网站上会遇到这种情况,例如,有一个固定的标题(这意味着当您滚动页面时,标题不会保留在页面顶部并且不可见,但始终可见)。一个例子是Facebook标题(顶部的蓝色标题包含搜索栏)。

我已多次遇到过这种情况,即使它位于该标题下方,该元素似乎也可见并启用。

要解决此问题,您可以使用移动到位于要单击的元素之上的元素。您可以使用Actions类来实现此目的。

Actions action = new Actions(driver);
//The element situated higher than the one you need to click
WebElement element = driver.findElement(By.xpath("xpath"));
action.moveToElement(element).build().perform();

答案 1 :(得分:2)

  

elementToBeClickable只是检查它是否可见并已启用

这绝对是正确的 - 预期条件不是基于实际Element Displayedness - 但是,isDisplayed()完全基于提到的WebDriver规范 - 在自定义预期条件中调用它:

public static ExpectedCondition<WebElement> isDisplayed(final By locator) {
    return new ExpectedCondition<WebElement>() {

        public WebElement apply(WebDriver driver) {
            WebElement element = webDriver.findElement(locator); 
            if (element.isDisplayed()) {
                return element;
            }
        }
    };
}

用法:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(isDisplayed(By.id("myid")));