Get Element Attribute Value in Robot Framework
In Robot Framework, you can use the Get Element Attribute keyword from the SeleniumLibrary to retrieve an HTML element's attribute value.
1. Syntax
${value}= Get Element Attribute locator attribute_name
- locator: ID, XPath, CSS selector, name, etc.
- attribute_name: HTML attribute such as
value,href,class,type,placeholder,title, etc.
2. Example: Get Input Attribute Value
HTML:
<input id="username"
type="text"
value="Sumit"
placeholder="Enter username">
Robot Framework:
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
Get Input Attribute Value
Open Browser https://example.com chrome
${value}= Get Element Attribute id=username value
${placeholder}= Get Element Attribute id=username placeholder
${type}= Get Element Attribute id=username type
Log To Console Input Value: ${value}
Log To Console Placeholder: ${placeholder}
Log To Console Type: ${type}
Close Browser
Expected output:
Input Value: Sumit
Placeholder: Enter username
Type: text
3. Get Different HTML Attributes
| Attribute | Robot Framework Keyword |
|---|---|
| value | Get Element Attribute id=username value |
| href | Get Element Attribute xpath=//a href |
| class | Get Element Attribute id=login class |
| id | Get Element Attribute xpath=//input id |
| type | Get Element Attribute id=username type |
| placeholder | Get Element Attribute id=username placeholder |
| title | Get Element Attribute xpath=//button title |
| src | Get Element Attribute xpath=//img src |
| name | Get Element Attribute xpath=//input name |
4. Get Textarea Attribute
${value}= Get Element Attribute id=description value
Log To Console ${value}
5. Get Element Attribute Using XPath
${value}= Get Element Attribute
... xpath=//input[@name='email']
... value
Log To Console ${value}
6. Difference Between Get Element Attribute and Get Text
| Keyword | Purpose |
|---|---|
Get Element Attribute |
Gets HTML attribute values. |
Get Text |
Gets visible text inside an element. |
Get Value |
Gets the value property of an input element. |
Get Element Attribute with value |
Gets the HTML value attribute. |
Example:
${attribute}= Get Element Attribute id=username value
${input_value}= Get Value id=username
${text}= Get Text xpath=//h1
Important: If you want the current value entered by the user in an input or textarea, prefer
Get Value. If you want an HTML attribute, useGet Element Attribute.

No comments:
Post a Comment