Back to Resources

Blog

Posted October 12, 2021

Selenium 4 - Element Attribute and Property Methods

Two new methods have been added to more precisely get a given element attribute or a property. We recommend using one of these new methods for performance and precision, especially if you are running tests on Sauce Labs.

quote

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.

A case where getDomProperty() returns false and getDomAttribute() returns null

A case where getDomProperty() returns false and getDomAttribute() returns null:

1
@Test
2
public void domPropertyReturnsFalseInsteadOfNullForBoolean() throws IOException {
3
URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
4
FirefoxOptions firefoxOptions = new FirefoxOptions();
5
firefoxOptions.setCapability("platformName", "Windows 10");
6
firefoxOptions.setCapability("browserVersion", "latest");
7
8
Map<String, Object> sauceOptions = new HashMap<>();
9
sauceOptions.put("name", "domPropertyReturnsFalseInsteadOfNullForBoolean");
10
sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
11
sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
12
firefoxOptions.setCapability("sauce:options", sauceOptions);
13
14
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
15
driver.get("http://watir.com/examples/forms_with_input_elements.html");
16
17
WebElement element = driver.findElement(By.id("new_user_interests_books"));
18
Assertions.assertEquals("true", element.getAttribute("checked"));
19
Assertions.assertEquals("true", element.getDomProperty("checked"));
20
21
element.click();
22
Assertions.assertNull(element.getAttribute("checked"));
23
Assertions.assertEquals("false", element.getDomProperty("checked"));
24
25
driver.quit();
26
}

Another case where the getDomProperty() Boolean result updates and getDomAttribute() does not

1
@Test
2
public void attributePropertyDoesNotUpdateString() throws IOException {
3
URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
4
FirefoxOptions firefoxOptions = new FirefoxOptions();
5
firefoxOptions.setCapability("platformName", "Windows 10");
6
firefoxOptions.setCapability("browserVersion", "latest");
7
8
Map<String, Object> sauceOptions = new HashMap<>();
9
sauceOptions.put("name", "attributePropertyDoesNotUpdateString");
10
sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
11
sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
12
firefoxOptions.setCapability("sauce:options", sauceOptions);
13
14
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
15
driver.get("http://watir.com/examples/forms_with_input_elements.html");
16
17
WebElement element = driver.findElement(By.id("new_user_occupation"));
18
Assertions.assertEquals("Developer", element.getAttribute("value"));
19
Assertions.assertEquals("Developer", element.getDomAttribute("value"));
20
Assertions.assertEquals("Developer", element.getDomProperty("value"));
21
22
element.clear();
23
element.sendKeys("Engineer");
24
Assertions.assertEquals("Engineer", element.getAttribute("value"));
25
Assertions.assertEquals("Developer", element.getDomAttribute("value"));
26
Assertions.assertEquals("Engineer", element.getDomProperty("value"));
27
28
driver.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.

Titus Fortner
Sr. Developer Experience Engineer, Sauce Labs
Diego Molina
Staff Software Engineer at Sauce Labs
Published:
Oct 12, 2021
Share this post
Copy Share Link
© 2024 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.