The most recent version of Selenium 3 had the getAttribute()
method, however it did not actually retrieve the Attribute value. The previous getAttribute()
method figured out what the user was most likely interested in between the Attribute value and the Property values and returned it.
Since an element attribute is different from an element property, the W3C WebDriver specification requires different implementations. Thanks to this, two new methods have been created for Selenium 4: getDomAttribute()
and getDomProperty()
.
Most of the time these two methods return the same thing (especially in Java where getDomProperty()
is returned as a String
value). You will find some code examples below, showing when you might want to use which method. Note that getAttribute()
is still available for backwards compatibility, but is implemented by sending a large blob of JavaScript to the remote endpoint. Where possible, it is recommended to use one of the new methods for performance and precision.
Here are some examples in Java; for additional examples in multiple languages, look at our Selenium 4 Documentation.
getDomProperty()
returns false and getDomAttribute()
returns nullA case where getDomProperty() returns false and getDomAttribute() returns null:
1@Test2public void domPropertyReturnsFalseInsteadOfNullForBoolean() throws IOException {3URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");4FirefoxOptions firefoxOptions = new FirefoxOptions();5firefoxOptions.setCapability("platformName", "Windows 10");6firefoxOptions.setCapability("browserVersion", "latest");78Map<String, Object> sauceOptions = new HashMap<>();9sauceOptions.put("name", "domPropertyReturnsFalseInsteadOfNullForBoolean");10sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));11sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));12firefoxOptions.setCapability("sauce:options", sauceOptions);1314RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);15driver.get("http://watir.com/examples/forms_with_input_elements.html");1617WebElement element = driver.findElement(By.id("new_user_interests_books"));18Assertions.assertEquals("true", element.getAttribute("checked"));19Assertions.assertEquals("true", element.getDomProperty("checked"));2021element.click();22Assertions.assertNull(element.getAttribute("checked"));23Assertions.assertEquals("false", element.getDomProperty("checked"));2425driver.quit();26}
getDomProperty() Boolean
result updates and getDomAttribute()
does not1@Test2public void attributePropertyDoesNotUpdateString() throws IOException {3URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");4FirefoxOptions firefoxOptions = new FirefoxOptions();5firefoxOptions.setCapability("platformName", "Windows 10");6firefoxOptions.setCapability("browserVersion", "latest");78Map<String, Object> sauceOptions = new HashMap<>();9sauceOptions.put("name", "attributePropertyDoesNotUpdateString");10sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));11sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));12firefoxOptions.setCapability("sauce:options", sauceOptions);1314RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);15driver.get("http://watir.com/examples/forms_with_input_elements.html");1617WebElement element = driver.findElement(By.id("new_user_occupation"));18Assertions.assertEquals("Developer", element.getAttribute("value"));19Assertions.assertEquals("Developer", element.getDomAttribute("value"));20Assertions.assertEquals("Developer", element.getDomProperty("value"));2122element.clear();23element.sendKeys("Engineer");24Assertions.assertEquals("Engineer", element.getAttribute("value"));25Assertions.assertEquals("Developer", element.getDomAttribute("value"));26Assertions.assertEquals("Engineer", element.getDomProperty("value"));2728driver.quit();29}
If you are using Sauce Labs, we recommend using the new methods as the commands will take less time to be executed, given that they won’t be sending a large blob of JavaScript over the internet.
Check out our comprehensive guide to Selenium 4 for more information.