How to know if a field is editable or not in selenium?

When you’re automating web applications using Selenium, you often need to check whether a field is editable or read-only. In this article, we’ll explore different ways to verify if a field is editable in Selenium, with clear examples in Java. Let’s dive in!

Note: We have also attached a video at the end of this article. In case you are more comfortable with the video version, please feel free to have a look

How can we tell if a field is editable? The key lies in the “readonly” attribute. If this attribute is present on an element, it means the field is locked for editing and can’t be modified.

non-editable field

So, if we find a way to know whether “readonly” is present for an element or not, then we can also determine the editability of a field.

We can do this in two ways:

Let’s go through each of these methods one by one.

The getAttribute() method from the WebElement interface helps us fetch the value of a specific attribute for an element.

Here, we’ll use it to get the value of the “readOnly” attribute. If it returns “true“, it means the field isn’t editable. Otherwise, the field can be edited.

We will use a non-editable element present on https://testkru.com/Elements/TextFields.

non-editable element
DOM of readonly element

The element we’re checking has the id “uneditable”. We’ll use this “id” to locate the element and then determine whether it’s editable or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class CodekruTest {
 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/TextFields");
 
        WebElement element = driver.findElement(By.id("uneditable"));
 
        // this will tell whether the field is editable or not
        System.out.println("Is text field non-editable: " + element.getAttribute("readonly"));
    }
}

Output –

Is text field non-editable: true

Since the readonly attribute was present on our element, the result we got was true.

JavascriptExecutor allows us to run JavaScript code directly within Selenium.

We can also use JavaScript to check the value of the readonly attribute. The script below will return true or false based on whether the field has the readOnly attribute set.

return arguments[0].readOnly

where arguments[0] is the element whose editability we want to check.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class CodekruTest {
 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/TextFields");
 
        WebElement element = driver.findElement(By.id("uneditable"));
 
        // this will tell whether the field is editable or not
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        System.out.println("Is text field non-editable: " + jse.executeScript("return arguments[0].readOnly", element));
    }
}

Output –

Is text field non-editable: true
Video Tutorials

This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles –
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *