- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to set “value” to input web element using selenium?
We can set value to input webelement using Selenium webdriver. We can take the help of the sendKeys method to enter text to the input field. The value to be entered is passed as an argument to the method.
Syntax
driver.findElement(By.id("txtSearchText")).sendKeys("Selenium");
We can also perform web operations like entering text to the edit box with Javascript Executor in Selenium. We shall use the executeScript method and pass argument index.value='<value to be entered>' and webelement as arguments to the method.
Syntax
WebElement i = driver.findElement(By.id("id")); JavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript("arguments[0].value='Selenium';", i);
Example
Code Implementation
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import java.util.concurrent.TimeUnit; public class SetValue{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement l=driver.findElement(By.cssSelector("input[id*='id']")); l.sendKeys("Selenium"); // obtain the value entered with getAttribute method System.out.println("Value entered is: " +l.getAttribute("value")); driver.close(); } }
Example
Code Implementation with Javascript Executor.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class SetValueJS{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement l=driver.findElement(By.cssSelector("input[id*='id']")); // Javascript Executor class with executeScript method JavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript("arguments[0].value='Selenium';", l); System.out.println("Value entered is: " +l.getAttribute("value")); driver.close(); } }
Output
- Related Articles
- Using Selenium Web Driver to retrieve value of a HTML input.
- Is it possible to manually set the attribute value of a Web Element using Selenium?
- How to get the attribute value of a web element in Selenium (using Java or Python)?
- How to find a radio button element by value using Selenium?
- How to verify color of a web element in Selenium Webdriver?
- How to get the input value of the first matched element using jQuery?
- Get value of an input box using Selenium (Python)
- How to set a value to a file input in HTML?
- How to verify the color and background color of a web element in Selenium?
- How to scroll to element with Selenium WebDriver using C#?
- How to programmatically set the value of a select box element using JavaScript?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- How to set Proxy in Firefox using Selenium WebDriver?
- How can we use JavaScript Executor to click and enter data to a web element in Selenium?
- How to handle web based alerts in Selenium?

Advertisements