- 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
Can I set any of the attribute value of a WebElement in Selenium?
We can set any attribute value of a webelement in Selenium. Selenium can run Javascript commands by the executeScript method. The command to be executed is passed as an argument to the method.
Next, we have to identify the element with the help of the Javascript method document.getElementsByClassname. It returns a list of elements, to point to the first element we shall add index [0]. To set the attribute we shall use the setAttribute method.
Syntax for setting the style attribute −
JavascriptExecutor j = (JavascriptExecutor) driver; js.executeScript ("document.getElementsByClassName('heading')[0].setAttribute('style', 'background-color: red')");
Let us set the background color of webelement to red.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class SetAttributeVal{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // Javascript executor to set background color to red JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript js.executeScript ("document.getElementsByClassName('heading')[0].setAttribute('style', 'background-color: red')"); driver.quit(); } }
Output
- Related Articles
- How to extract the text of a webelement in Selenium?
- Is it possible to manually set the attribute value of a Web Element using Selenium?
- How to get HTML code of a WebElement in Selenium?
- How to extract the attribute value of an element in Selenium?
- Get HTML Source of WebElement in Selenium WebDriver using Python.
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- Get value of any attribute from XML data in JavaScript?
- How can I manually set proxy settings in Python Selenium?
- How to get an attribute value of an element in Selenium Webdriver?
- How to get the attribute value of a web element in Selenium (using Java or Python)?
- How to fetch values from a webelement in Selenium with python?
- How to get an attribute value from a href link in selenium?
- How to get text from Selenium element WebElement object?
- How can I set the default value of my Tkinter Scale widget slider to 100?
- How can I set the default value for an HTML element?

Advertisements