0
0
Selenium Javatesting~5 mins

Getting and setting attributes in Selenium Java

Choose your learning style9 modes available
Introduction

We get and set attributes to check or change properties of web elements during tests. This helps us verify the page works as expected.

Check if a button is disabled before clicking it.
Verify the placeholder text in an input box.
Change the value of an input field before submitting a form.
Confirm the href link of an anchor tag.
Update an element's attribute to simulate user interaction.
Syntax
Selenium Java
String value = element.getAttribute("attributeName");
// element.setAttribute("attributeName", "newValue"); // Note: setAttribute is not directly available in Selenium WebDriver

In Selenium WebDriver, getAttribute is used to read an attribute's value.

There is no direct setAttribute method in Selenium WebDriver; to set attributes, you use JavaScript execution.

Examples
Gets the placeholder text from an input field.
Selenium Java
String placeholder = inputElement.getAttribute("placeholder");
Gets the URL from a link's href attribute.
Selenium Java
String href = linkElement.getAttribute("href");
Sets the value attribute of an input field using JavaScript.
Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('value', 'new text')", inputElement);
Sample Program

This test opens a page, reads the placeholder text of an input box, sets a new value using JavaScript, then reads the value to confirm the change.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class AttributeTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/form");

            WebElement input = driver.findElement(By.id("username"));

            // Get placeholder attribute
            String placeholder = input.getAttribute("placeholder");
            System.out.println("Placeholder before: " + placeholder);

            // Set value attribute using JavaScript
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].setAttribute('value', 'testuser')", input);

            // Get value attribute after setting
            String value = input.getAttribute("value");
            System.out.println("Value after setting: " + value);

        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always prefer getAttribute to check element properties instead of reading visible text when testing attributes.

To change attributes, use JavaScript execution because Selenium WebDriver does not provide a direct method.

Remember to close the browser with driver.quit() to free resources.

Summary

Get attributes to verify element properties.

Set attributes using JavaScript execution in Selenium.

Use these to simulate user input and check element states in tests.