<<Previous Post << Complete Tutorial>> NextPost>>
WebDriverIO – getAttribute Command
getAttribute command is used to get an attribute from a DOM-element based on the attributes that a particular element contains.
Syntax:- $(selector).getAttribute(attributeName)
Pre-requisite: Have ‘Visual Studio Code’ IDE installed in your machine (Click here to install if not installed in your machine)
Here is the small Exercise where we will pick an attribute of the form element and will print the value present inside that attribute
1) Navigate to http://omayo.blogspot.com/
2) Find out the element and attributes associated with whose data you want to fetch and display. Here, I want to display the text present in the name attribute.
3) Display the results.
Practical Demonstration:
Following are the steps to Perform above exercise (i.e., to fetch data/Text from attribute associated with the element and then display it)
Pre-requisite: Download the project from here (Click here to Download) and Unzip it.
1) Open Visual Studio Code on your system.
2) Click on File and select Open Folder. Select the project folder on which your workspace is present.
3) Expand ‘test’ from the Explorer, right-click on the displayed ‘specs’ and select ‘New file’ option as shown below :
4) Give any name of the newly created file say ‘getAttribute_command_test.js’ and copy the below-given code into the newly created file as shown below and save the file:
The following is the WebDriverIO sample code for using getAttribute() Command of WebDriverIO on attributes associated with elements present on the page on ‘omayo.blogspot.com’ web application.
You can understand this code, by reading the comments provided in the below code:
describe('This Example show usage of getAttribute() command in WebDriverIO',()=> { it('should give us true for enabled Element and False for disabled Element in DOM',()=> { browser.url('http://omayo.blogspot.com'); const form = $('form'); const attributeValue = form.getAttribute('name'); //name is one of the Attribute of DOM Element form console.log('value of attribute is ' + attributeValue); // outputs: form1 }); });
5) Click on ‘wdio.conf.js’ to open in the IDE, and make sure the Test file name is updated properly and save it:-
6) Click on Terminal and Open New Terminal.
7) Git Bash Terminal will look like this as below:-
8) Start Selenium Standalone server using the command as given below.
./node_modules./bin/selenium-standalone start

9) Open Another Terminal like we did in step 6 and run below command.
./node_modules./bin/wdio wdio.conf.js
Observe that the ‘getAttribute_command_test.js’ will get executed successfully. The chrome Browser will launch, then it will look out for the element associated with the attribute based on the XPath and respective action will be taken by WebDriverIO Automation code.
And also, the result will be displayed as ‘Passed’ in the terminal as shown below:-

This is how we can take the help of getAttribute() command of WebDriverIO to display the data/text present in attributes associated with elements on the DOM. I will explain other commands to perform different operations on WebPages using WebDriverIO in the upcoming Articles.
Next Steps:
> To learn more about WebDriverIO, continue to the next post (Click on Next Post link below)
> Check complete WebDriverIO Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning?
Connect to me on LinkedIn (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.
<<Previous Post << Complete Tutorial>> NextPost>>