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.

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:

@Test
public void domPropertyReturnsFalseInsteadOfNullForBoolean() throws IOException {
	URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
	FirefoxOptions firefoxOptions = new FirefoxOptions();
	firefoxOptions.setCapability("platformName", "Windows 10");
	firefoxOptions.setCapability("browserVersion", "latest");

         Map<String, Object> sauceOptions = new HashMap<>();
	sauceOptions.put("name", "domPropertyReturnsFalseInsteadOfNullForBoolean");
	sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
	sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
	firefoxOptions.setCapability("sauce:options", sauceOptions);

         RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
	driver.get("http://watir.com/examples/forms_with_input_elements.html");

         WebElement element = driver.findElement(By.id("new_user_interests_books"));
	Assertions.assertEquals("true", element.getAttribute("checked"));
	Assertions.assertEquals("true", element.getDomProperty("checked"));

         element.click();
	Assertions.assertNull(element.getAttribute("checked"));
	Assertions.assertEquals("false", element.getDomProperty("checked"));

        driver.quit();
}

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

@Test
public void attributePropertyDoesNotUpdateString() throws IOException {
	URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
	FirefoxOptions firefoxOptions = new FirefoxOptions();
	firefoxOptions.setCapability("platformName", "Windows 10");
	firefoxOptions.setCapability("browserVersion", "latest");

         Map<String, Object> sauceOptions = new HashMap<>();
	sauceOptions.put("name", "attributePropertyDoesNotUpdateString");
	sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
	sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
	firefoxOptions.setCapability("sauce:options", sauceOptions);

	RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
	driver.get("http://watir.com/examples/forms_with_input_elements.html");

	WebElement element = driver.findElement(By.id("new_user_occupation"));
	Assertions.assertEquals("Developer", element.getAttribute("value"));
	Assertions.assertEquals("Developer", element.getDomAttribute("value"));
	Assertions.assertEquals("Developer", element.getDomProperty("value"));

	element.clear();
	element.sendKeys("Engineer");
	Assertions.assertEquals("Engineer", element.getAttribute("value"));
	Assertions.assertEquals("Developer", element.getDomAttribute("value"));
	Assertions.assertEquals("Engineer", element.getDomProperty("value"));

	driver.quit();
}

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.





Written by

Diego Molina and Titus Fortner


Topics

SeleniumAutomated testing