Is there any other way(not sendKeys) to enter text into textbox using selenium webdriver ?
-
4Why would there be another way? What is the problem with sendKeys? API designers rarely provide 5 methods doing the same thing. – JB Nizet Nov 16 '14 at 08:49
-
One interviewer asked this question... – Vasantha Nov 16 '14 at 10:00
7 Answers
Any other way is only to use native Javascript action to enter the value in the text box:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById("textbox_id").value='new value';);

- 2,286
- 4
- 38
- 64
-
I'm getting error like this... Exception in thread "main" org.openqa.selenium.WebDriverException: document.getElementById(...) is null Command duration or timeout: 31 milliseconds.Can you tell me the solution please ? – Vasantha Nov 16 '14 at 12:26
-
-
@Vasntha, try mine code its tested one
driver.get("http://www.qajudge.com/");
WebElement cssValue= driver.findElement(By.xpath(".//*[@id='s']"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('s').value='Virender Testing
sending'");

- 1
- 1
WebElement name=driver.findElement(By.name("username"));
((JavascriptExecutor)driver).executeAsyncScript("arguments[0].value='admin'",name);
-
3Welcome to stack overflow :-) code-only answers aren't useful for the community. Please look at [answer] – JimHawkins Jul 13 '17 at 10:45
Try below one, which I tried in Gmail and it works
System.setProperty("webdriver.chrome.driver", "you .exe path here");
WebDriver driver = new ChromeDriver();
driver.get("https://accounts.google.com/");
driver.manage().window().maximize();
Thread.sleep(1000);
//Next button element
WebElement nextBtn = driver.findElement(By.id("identifierNext"));
JavascriptExecutor js = (JavascriptExecutor)driver;
//code to enter value in the email textbox
js.executeScript("document.getElementById('identifierId').value='testemail'");
//code to click on next button
js.executeScript("arguments[0].click();", nextBtn);

- 515
- 1
- 5
- 11
Yes, use javascript executer. Here, I have written a code that uses XPath to capture the element.
WebElement webelement = driver.findElement(By.xpath("your xpath");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value='Techndeck';",webelement );

- 91
- 4
Enter text using javascriptexecutor How to use Javascriptexecutor for entering text in an element which , I want to enter text in an Textbox which does not take the input through SendKeys. I proceeded with using Javascriptexecutor to enter the text Example 2. Get/Retrieve text with JavascriptExecutor. In this example, we are accessing a website and trying to retrieve the text written on the login button using JavascriptExecutor
How To Send Texts And Get Texts Using JavascriptExecutor?, So in such case, we use JavascriptExecutor of Selenium WebDriver library to handle Well, let's start with handling sending text data to the text area using If you still have any confusions please write in the comment below. This is another technique to send texts using JavascriptExecutor in Selenium WebDriver. Here is the sample code: ADA.
WebElement webl = driver.findElement(By.xpath(“xpath_expression”));JavascriptExecutor js = (JavascriptExecutor)driver;js.executeScript(“arguments[0].value=’SynTest’;”, webl);
1.How to enter text using JavaScript in Selenium WebDriver, Following are the commands you can use to enter text using javascript. You don't need to use sendkeys always. Syntax: JavascriptExecutor jexe To enter text within a textbox identified through an xpath you can use the following notation:
(JavascriptExecutor) driver.executeScript("document.evaluate(xpathExpresion, document, null, 9, null).singleNodeValue.innerHTML="+ DesiredText);

- 95
- 2
- 7
driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account");
JavascriptExecutor js= (JavascriptExecutor)driver;
js.executeScript("document.getElementById('email').value='Arun Singh'");
js.executeScript("document.getElementById('passwd').value='arunsingh'");

- 1
- 1