Selenium 4 - Element Attribute and Property Methods

Sauce AI for Test Authoring: Move from intent to executing in min.|xBack to ResourcesBlogPosted 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 method for performance and precision, peculiarly if you are running tests on Sauce Labs.

quote

The most recent version of Selenium 3 had thegetAttribute ()method, however it did not really retrieve the Attribute value. The previousgetAttribute ()method figured out what the user was well-nigh likely interested in between the Attribute value and the Property values and retrovert it.

Since an element attributeis different from anelement property, the W3C WebDriver specification requires different implementations. Thanks to this, two new method have been created for Selenium 4:getDomAttribute () and getDomProperty ().

Most of the time these two method render the same thing (specially in Java wheregetDomProperty ()is returned as aStringvalue). You will find some code examples below, show when you might need to use which method. Note thatgetAttribute ()is still available for backwards compatibility, but is implemented by sending a tumid blob of JavaScript to the remote endpoint. Where possible, it is recommended to use one of the new method for execution and preciseness.

Here are some examples in Java; for additional illustration in multiple languages, face at our.

A case wheregetDomProperty ()return false andgetDomAttribute ()returns zero

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

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

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

Another case where thegetDomProperty () Booleanresult updates andgetDomAttribute () does not

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

If you are employ Sauce Labs, we advocate apply the new methods as the commands will occupy less clip to be executed, given that they won ’ t be sending a large blob of JavaScript over the internet.

Check out our comprehensive guide toSelenium 4for 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
LinkedIn
© 2026 Sauce Labs Inc., all right allow. SAUCE and SAUCE LABS are register earmark owned by Sauce Labs Inc. in the United States, EU, and may be registered in former jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free