- 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
How to input text in the text box without calling the sendKeys() using Selenium?
We can input text in the text box without the method sendKeys with thehelp of the JavaScript Executor. Selenium executes JavaScript commands with the help of the executeScript method.
The JavaScript command to be run is passed as parameter to the method. To enter text we shall first identify the input box with the JavaScript method document.getElementById. Then we have to apply the value method on it.
Let us enter text Selenium in the below input box −
Syntax
JavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript ("document.getElementById('gsc-i-id1').value='Selenium'");
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 JsEnterText{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/index.htm"); // JavaScript Executor to enter text JavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript ("document.getElementById('gsc-i-id1').value='Selenium'"); WebElement l = driver.findElement(By.id("gsc-i-id1")); String s = l.getAttribute("value"); System.out.println("Value entered is: " + s); driver.quit(); } }
Output
- Related Articles
- How to key in values in input text box with Javascript executor in Selenium with python?
- How do you enter text in the edit box in Selenium?
- How to create a text input box with Pygame?
- How to find an element using the “Link Text/Partial Link Text” in Selenium?
- How to upload a file in Selenium with no text box?
- How to simulate pressing enter in html text input with Selenium?
- How to insert text at the beginning of the text box in Tkinter?
- How to get the text from a website using selenium?
- Text in Transparent Box using CSS3
- How to limit input text length using CSS3?
- How to find text box height in IText using FabricJS?
- How to Convert Text in Text Box to Cell Content in Excel?
- How to take input in a text widget and display the text in tkinter?
- How to disable spell check from input box and text area in HTML forms?
- How to place cursor position at end of text in text input field using JavaScript?

Advertisements