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.
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@Test2public void domPropertyReturnsFalseInsteadOfNullForBoolean () shed IOException {3URL gridUrl = new URL (& quot; https: //ondemand.us-west-1.saucelabs.com:443/wd/hub & quot;);4FirefoxOptions firefoxOptions = new FirefoxOptions ();5firefoxOptions.setCapability (& quot; platformName & quot;, & quot; Windows 10 & quot;);6firefoxOptions.setCapability (& quot; browserVersion & quot;, & quot; latest & quot;);78Map & lt; String, Object & gt; sauceOptions = new HashMap & lt; & gt; ();9sauceOptions.put (& quot; name & quot;, & quot; domPropertyReturnsFalseInsteadOfNullForBoolean & quot;);10sauceOptions.put (& quot; username & quot;, System.getenv (& quot; SAUCE_USERNAME & quot;));11sauceOptions.put (& quot; accessKey & quot;, System.getenv (& quot; SAUCE_ACCESS_KEY & quot;));12firefoxOptions.setCapability (& quot; sauce: options & quot;, sauceOptions);1314RemoteWebDriver driver = new RemoteWebDriver (gridUrl, firefoxOptions);15driver.get (& quot; http: //watir.com/examples/forms_with_input_elements.html & quot;);1617WebElement constituent = driver.findElement (By.id (& quot; new_user_interests_books & quot;));18Assertions.assertEquals (& quot; true & quot;, element.getAttribute (& quot; checked & quot;));19Assertions.assertEquals (& quot; true & quot;, element.getDomProperty (& quot; see & quot;));2021element.click ();22Assertions.assertNull (element.getAttribute (& quot; control & quot;));23Assertions.assertEquals (& quot; false & quot;, element.getDomProperty (& quot; ascertain & quot;));2425driver.quit ();26}
Another case where thegetDomProperty () Booleanresult updates andgetDomAttribute () does not
1@Test2public void attributePropertyDoesNotUpdateString () throws IOException {3URL gridUrl = new URL (& quot; https: //ondemand.us-west-1.saucelabs.com:443/wd/hub & quot;);4FirefoxOptions firefoxOptions = new FirefoxOptions ();5firefoxOptions.setCapability (& quot; platformName & quot;, & quot; Windows 10 & quot;);6firefoxOptions.setCapability (& quot; browserVersion & quot;, & quot; latest & quot;);78Map & lt; String, Object & gt; sauceOptions = new HashMap & lt; & gt; ();9sauceOptions.put (& quot; name & quot;, & quot; attributePropertyDoesNotUpdateString & quot;);10sauceOptions.put (& quot; username & quot;, System.getenv (& quot; SAUCE_USERNAME & quot;));11sauceOptions.put (& quot; accessKey & quot;, System.getenv (& quot; SAUCE_ACCESS_KEY & quot;));12firefoxOptions.setCapability (& quot; sauce: options & quot;, sauceOptions);1314RemoteWebDriver driver = new RemoteWebDriver (gridUrl, firefoxOptions);15driver.get (& quot; http: //watir.com/examples/forms_with_input_elements.html & quot;);1617WebElement factor = driver.findElement (By.id (& quot; new_user_occupation & quot;));18Assertions.assertEquals (& quot; Developer & quot;, element.getAttribute (& quot; value & quot;));19Assertions.assertEquals (& quot; Developer & quot;, element.getDomAttribute (& quot; value & quot;));20Assertions.assertEquals (& quot; Developer & quot;, element.getDomProperty (& quot; value & quot;));2122element.clear ();23element.sendKeys (& quot; Engineer & quot;);24Assertions.assertEquals (& quot; Engineer & quot;, element.getAttribute (& quot; value & quot;));25Assertions.assertEquals (& quot; Developer & quot;, element.getDomAttribute (& quot; value & quot;));26Assertions.assertEquals (& quot; Engineer & quot;, element.getDomProperty (& quot; value & quot;));2728driver.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.
Sr. Developer Experience Engineer, Sauce Labs
Staff Software Engineer at Sauce Labs
Share this post
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 FreeTest 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
