简体   繁体   English

如何使用 Java 在 Selenium WebDriver 中检查元素是否可点击

[英]How to check if element is clickable in Selenium WebDriver using Java

I have to check that a text box accepts only a particular number of characters.我必须检查一个文本框是否只接受特定数量的字符。 If the characters exceed, it doesn't give any error but only turn the text in red.如果字符超过,它不会给出任何错误,而只会将文本变为红色。 Another text box simply doesn't accept additional characters.另一个文本框根本不接受其他字符。 How do I verify that text box doesn't accept more characters in both cases?我如何验证文本框在两种情况下都不接受更多字符?

One option I have, is check if the 'Save' button which is visible on the page is clickable.我的一个选择是检查页面上可见的“保存”按钮是否可点击。 How do I do this with Selenium WebDriver in Java?如何使用 Java 中的 Selenium WebDriver 执行此操作?

Just write the below method and call it whenever you want to check whether the element is clickable or not.只需编写以下方法并在您想检查元素是否可点击时调用它。 Pass the required arguments also.还传递所需的参数。

public static boolean isClickable(WebElement el, WebDriver driver) 
    {
        try{
            WebDriverWait wait = new WebDriverWait(driver, 6);
            wait.until(ExpectedConditions.elementToBeClickable(el));
            return true;
        }
        catch (Exception e){
            return false;
        }
    }

You can use isEnabled() to check if an element is enabled or not.您可以使用isEnabled()来检查元素是否已启用。

driver.findElement(By.xpath("//path/to/element").isEnabled();

This will return true if the button is clickable.如果按钮可点击,这将返回true

If by clickable you mean not disabled, you can use WebElement.isEnabled() .如果可点击意味着未禁用,则可以使用WebElement.isEnabled()

About the text input that doesn't accept additional characters, if you want to detect that instead, it depends on how that constraint is enforced.关于不接受附加字符的文本输入,如果您想检测它,则取决于该约束的实施方式。 For example if it's done through a maxlength attribute you can try to read that attribute from your input element ( WebElement.getAttribute(String) ).例如,如果它是通过maxlength 属性完成的,您可以尝试从input元素 ( WebElement.getAttribute(String) ) 中读取该属性。 In this case you would know in advance how many characters you can send to the textbox.在这种情况下,您会提前知道可以发送到文本框的字符数。

About the text input that turns the text to red, if you want to detect that you should first find out how the text is turned to red;关于把文字变成红色的文字输入,如果要检测应该先搞清楚文字是怎么变成红色的; probably it does that by setting a CSS class or style attribute to your input element, in which case you can try to read that attribute from the element.可能是通过为input元素设置 CSS classstyle属性来实现的,在这种情况下,您可以尝试从元素中读取该属性。

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

相关问题 如何使用Selenium WebDriver检查按钮是否可单击 - How to check if the button is clickable using selenium webdriver 如何使用Selenium Webdriver等待元素不可单击? - How to wait for an element NOT to be clickable using Selenium Webdriver? Java Selenium Webdriver:元素不可单击 - Java Selenium Webdriver: Element not clickable 如何检查按钮是否在java,selenium webdriver.ps中可点击:检查是否可点击不要等到它可点击 - how to check if a button is clickable in java,selenium webdriver.ps:check if clickable not wait untill it is clickable 检查 Selenium Java 中的元素是否可点击 - Check if element is clickable in Selenium Java 如何使用Selenium Java WebDriver选中复选框? - How to check the checkboxes using the Selenium Java WebDriver? 使用带有 Java 的 Selenium WebDriver 检查元素不存在的最佳方法 - Best way to check that element is not present using Selenium WebDriver with java Selenium Webdriver(Java)-如何检查findelement中是否存在标签元素 - Selenium Webdriver (Java) - how to check if tag element exists in findelement 如何向下滚动特定的div以查找元素并通过Java中的Selenium WebDriver单击它 - How to scroll down of specific div to locate an element and make it clickable via selenium webdriver in java 如何使用 Selenium WebDriver 使用 Java 确定元素的颜色? - How to determine the color of an element using Java using Selenium WebDriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM