Selenium Webdriver - check write protected text field?

How can we check if a field is write-protected (i.e. readonly) in Selenium using Java code?

Best wishes

+6
source share
8 answers

You can try to write something via sendkeys () and verify that the attribute value of the text field has not been changed.

+4
source
  • isEnabled () has no common things to read.
  • String attribute = element.getAttribute ("readonly"); won't fail your test, even "readonly" is missing. In this case, it returns null , but we need an exception.

Use this:

WebElement some_element = driver.findElement(By.id("some_id")); String readonly = some_element.getAttribute("readonly"); Assert.assertNotNull(readonly); 

DO NOT check getAttribute ("readonly"). equals ("true") or similar, it may be different in different browsers. (readonly = "readonly" in IE, readonly = "" in FF, etc.)

+12
source

The WebElement interface has an isEnabled function.

+3
source

with selenium-java 2.21.0 you can check if it is enabled

 driver.findElement(By.id("...")).isEnabled() 
+1
source

This is a late answer, but since I am working on it recently, I would like to share my solution.

I implemented a check method if the text field is read-only or not. I am working on a Django project, so the code is in Python, but I find it easy to port to Java:

 from django.contrib.staticfiles.testing import StaticLiveServerTestCase class MyTest(StaticLiveServerTestCase): def setUp(self): self.browser = webdriver.Firefox() def _is_text_box_read_only(self, web_element): old_text = web_element.get_attribute("value") web_element.send_keys("test") new_text = web_element.get_attribute("value") web_element.clear() web_element.send_keys(old_text) # Resume to the previous text return old_text == new_text 

Note that:

  • To get the entered text, you need to use the value attribute.
  • It is better to return to the previous text after testing.
  • Code is just an excerpt that shows the main ideas. It cannot be started.
+1
source

Since I don’t know why you need this check, I will post some examples that may be useful.

 driver.findElements(By.cssSelector("input:not([readonly='readonly'])[type='text']")); 

=> returns all text input fields that are edited

 WebElement element = driver.findElement(By.id("username"); //can fail if the attribute is not there String attribute = element.getAttribute("readonly"); 

=> a catch try block may be required

0
source

Below is the code that works best for me. I hope it will be useful

  WebElement fieldName = driver.findElement(By.id("enter_id")); fieldName.sendKeys("abc"); String fieldNameVal = fieldName.getAttribute("value"); if(fieldNameVal.contentEquals("abc")){ System.out.println("Field is editable"); } else{ System.out.println("Field is non editable" + fieldNameVal); } 
0
source

I know that they answered. but unfortunately, when I encountered a similar problem, I had to spend some time to find the right solution.
I agree that .getAttribute ("readonly") returns null, but to handle it> I wrote something similar to this:

 public boolean isreadOnly(element) { Boolean readOnly = false; readOnly = ((element.getAttribute("disabled") != null) || (element.getAttribute("readonly") != null)); return readOnly; } 

and call above:

 WebElement text = driver.findElement("locator")); return isReadOnly(text); 
0
source

Source: https://habr.com/ru/post/911082/


All Articles