InterviewSolution
| 1. |
How to get attributes? |
|
Answer» In automation, there are cases where you want to get the values of the attribute and then perform any action The attribute value will be NULL for the TAG that does not exist. To add extra meaning to a web element, a web developer defines the attribute. Attributes have additional information about an HTML element and come in name="value" pairs. Attributes are defined by HTML. We retrieve the values of attributes of web elements to verify test cases. LET us consider a movie ticket booking application. Where available seat colour will be green and booked seat colour will be red, so, to check whether a seat is available or already booked, we need to FETCH attribute (colour) value through the script and based on the value we need to perform operations. To achieve this Selenium WebDriver provides a method called getAttribute() which is declared in WebElement interface. It returns the value of the web element a String of the Web element. getAttribute() method returns either “true” or null for attributes whose value is Boolean. getAttribute() method work on specific web elements, so first you need to locate a web element and then call getAttribute() method on it by specifying the attribute for which value you require. To get the attribute value using selenium WebDriver, we can use 'element.getAttribute(attributeName)'. WebElement searchTextBox= driver.findElement(By.id("lst-ib")); // retrieving html attribute value using getAttribute() method String titleValue=searchTextBox.getAttribute("title"); |
|