Updated April 3, 2023
Introduction to Selenium Get Attribute
The selenium get attribute() is one of the default method for declaring the WebElement interface that is imported using the Selenium driver interface also the web element’s attributes are passed only the string type of values but when we used getAttribute() method it will handle boolean type of values and it will return the same as output result like true or false, yes or no during the test automation the QAs might need to be fetch the user-defined attributes values that specifically with the web elements for validating the test scripts done by the tester during the runtime.
What is selenium get attribute?
The WebElement interface declares the getAttribute() function, which returns the value of the web element’s attribute as a string. The getAttribute() method returns true or false or sometimes it shows null for displayed in the attributes with boolean values. More ever the developers or QAs will retrieve the values for these types of attributes using the getAttribute() method it plays a vital role and it has important for decision-making scenarios because the real-time applications user data will be more and it creates n number of queues and stacks, traces along with the chain mechanism so this method will helps to fetching the data.
How to use Attribute selenium get Attribute?
Selenium’s getAttribute() method works on particular web components. Because in real-time applications it takes n number of scenarios for storing and retrieving the data the transaction from the front end to backend and vice versa repeatedly. The user or technical persons like QA or others must first locate the web components before calling the getAttribute() method and specifying the attributes for which values are needed then only it will fetch the accurate data and displayed it on the UI. This Selenium locators and other features can be easily referenced to learn how to locate web items and utilised with the applications. We’ll use the properly titled getAttribute() function for to get the attribute’s value, it supplying the name of the attribute whose value already we’re tried and trying forward to retrieve as an argument. Because the method returns a promise that is either true or false condition then it will use then() method for to report the value as returned one. By using this method sometimes we can able to rerun the codes and the attribute for displayed the value of the attribute elements and returned as the string type if suppose the element does not return the attribute with the specified name values then it shows as the null.
Selenium get attribute method
The IWebElement Interface has a function called getAttribute() method and IWebElement interface aids in the execution of all web page element interactions and user operations. The getAttribute() method helps and it mainly focused on to retrieves the value of any web element’s attribute and returns it as a String. This method will return True or false also if an attribute has a Boolean value. So the method will also return null value if there is no attribute we will go out of over all of the different attributes and then values may return later, but initially, we can over it for why they need to utilise the getAttribute() method in the operation it may suffix along with the performance issue for more number of data through the web operations. We also double-check the user data with the specified placeholder context like input text boxes, labels field as well as the image source and other field dimensions. So that the getAttribute() method will comes to the rescue for this situation to obtain the values of an attribute it may be simply locate the web element that contains it for utilising and accessing the user data.
Example #1
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.manage().window().maximize();
dr.get("https://www.google.com/");
String st = dr.findElement(By.xpath("//input[@class='gNO89b'][@type='submit']")).getAttribute("value");
System.out.println(st);
dr.get("https://www.facebook.com/");
WebElement st1 = dr.findElement(By.xpath("//button[@class='_42ft _4jy0 _6lth _4jy6 _4jy1 selected _51sy'][@type='submit']"));
String st12=st1.getAttribute("name");
System.out.println(st12);
}
}
Sample Output:
The above example we used getAttribute() method in different stages like we sued two request getURLs() for getting the input values from the web pages. Initially we imported all the packages which based and required from the file and tester like if we need webdriver we must imported the web deriver packages and then chromedriver() , if suppose we require firefox(), Safari() all the browser drivers must be imported on initial stage. Then we need to create the object of the browser driver like here I used chromedriver() so this will setup and created the object instance of whichever we required and used in the below areas. Then we get the URL here I used google.com for to perform the automation testing and then I find the XPath of the specified Attribute and using getAttribute() method we can get the values of the specified attribute. Other than again I get the current Url-like user request url here facebook.com is used for to perform the other getAttribute() method operations. So we can perform n number of requirement for to testing this operations using selenium default methods.
Example #2
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.manage().window().maximize();
dr.get("https://onepagelove.com/tag/demo");
WebElement st1 = dr.findElement(By.xpath("//input[@class='search-field'][@type='search']"));
String str2= st1.getAttribute("placeholder");
System.out.println(str2);
}
}
Sample Output:
The above example we sued same getAttribute() in the single web page called https://onepagelove.com/tag/demo and then we can handle the XPath in search class text box by using the getAttribute() method we can get the value of the placeholder attribute.
Conclusion
In selenium framework and the ide which used for handle the web-based applications for to perform the automation testing. Among that some default classes and methods are used to execute the test scripts from time frame automated like that getAttribute() basic method for fetching the values from the specified WebElement or attribute.
Recommended Articles
We hope that this EDUCBA information on “Selenium Get Attribute” was beneficial to you. You can view EDUCBA’s recommended articles for more information.