
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- How do you enter text in the edit box in Selenium?
- How to insert text at the beginning of the text box in Tkinter?
- How to key in values in input text box with Javascript executor in Selenium with python?
- Text in Transparent Box using CSS3
- How to upload a file in Selenium with no text box?
- How to get the text from a website using selenium?
- Clear text from text area using selenium WebDriver.
- How to find an element using the “Link Text/Partial Link Text” in Selenium?
- How to simulate pressing enter in html text input with Selenium?
- How to take input in a text widget and display the text in tkinter?
- How to limit input text length using CSS3?
- How to get the input from the Tkinter Text Widget?
- How to extract the text of a webelement in Selenium?
- How to clear the text entered in Selenium with python?
- How can I consistently remove the default text from an input element with Selenium?
Advertisements