如何检查非输入文本元素在 Selenium 中是否可单击? (Java)

How can I check if a non-input text element is clickable in Selenium? (Java)

我正在尝试检查非输入文本元素(只能查看但不能编辑)的可点击性。我有一个测试,我想断言无法单击页面上显示的仅查看文本元素(例如:名字)。

我已经尝试使用 isEnabled() 方法来检查是否启用了仅视图文本元素,但是断言没有正确发生。

这是 Bobcat Selenium 代码

步骤定义代码:

@Then("^I should verify that the First Name is not clickable$")
    public void iShouldVerifyThatTheFirstNameIsNotClickable() {
        assertEquals("Error: First Name is clickable", true, 
fullName.verifyClick());
}

页面对象代码:

public boolean verifyClick() {
        if (firstName.isEnabled()) {
            return true;
        }
        else {
                return false;
        }
}

预期结果:由于 firstName 是仅供查看的元素,因此 verifyClick() 方法的结果应该为假,因此我的 @Then("^I should verify that the First Name is not clickable$") 结果应该失败,因为断言失败。

实际结果:@Then("^I should verify that the First Name is not clickable$")结果成功。

selenium Java 绑定的一部分可能对您有用。在 ExpectedConditions you'll find a function called elementToBeClickable(). This returns a boolean that's false whenever the element is not clickable for any reason, and true when it can receive a click. So you just want to wait and see if that function returns true. Selenium handles that as well with the WebDriverWait class.

所以你需要导入这两个,然后你可以这样做:

//setting the timeout for our wait to be 20 seconds (you can use whatever you want)
WebDriverWait myWaitVar = new WebDriverWait(driver,20); 
try {
    WebElement myElement = myWaitVar.until(ExpectedConditions.elementToBeClickable(firstName)));
    //assert test failed!
}
catch(timeoutException timeout) {
    //whatever you want to do when the element is not clickable
}