- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it possible to manually set the attribute value of a Web Element using Selenium?
Yes it is possible to manually set the attribute value of a web element in Selenium webdriver using the JavaScript Executor. Selenium can run JavaScript commands with the help of the executeScript method.
First, we shall identify the element on which we want to manually set the attribute value with the JavaScript command document.getElementsByClassname. Next to set the attribute we have to use the setAttribute method.
Let us modify the background color of the button CHECK IT NOW to yellow. By default it is green on the page.
The can be done by setting the style attribute of the background-color to yellow.
Syntax
JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript ("document.getElementsByClassName('mui-btn')[0].setAttribute('style', " + "'background-color: yellow')");
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class BackGroundColr{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // Javascript executor to modify background color JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript ("document.getElementsByClassName('mui-btn')[0].setAttribute('style', " + "'background-color: yellow')"); } }
Output
- Related Articles
- How to set “value” to input web element using selenium?
- How to get the attribute value of a web element in Selenium (using Java or Python)?
- How to extract the attribute value of an element in Selenium?
- Using Selenium Web Driver to retrieve value of a HTML input.
- Can I set any of the attribute value of a WebElement in Selenium?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- How to get an attribute value of an element in Selenium Webdriver?
- How to find an element using the attribute “id” in Selenium?
- How to find an element using the attribute “name” in Selenium?
- How to get attribute of element from Selenium?
- How to find an element using the attribute “class name” in Selenium?
- How to verify an attribute is present in an element using Selenium WebDriver?
- How to verify color of a web element in Selenium Webdriver?
- How can I manually set proxy settings in Python Selenium?
- How to find an element using the attribute “HTML tag name” in Selenium?

Advertisements