Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set value of input instead of sendKeys() - Selenium WebDriver nodejs

I have a long string to test and sendKeys() takes too long. When I tried to set the value of the text the program crashes. I know the Selenium sendKeys() is the best way to test the actual user input, but for my application it takes too much time. So I am trying to avoid it.

Is there a way to set the value right away?

See this quick example:

var webdriver = require('selenium-webdriver');  var driver = new webdriver.Builder().    withCapabilities(webdriver.Capabilities.chrome()).       build();  driver.get('http://www.google.com');  // find the search input field on google.com inputField = driver.findElement(webdriver.By.name('q'));  var longstring = "test"; // not really long for the sake of this quick example  // this works but is slow inputField.sendKeys(longstring);  // no error but no values set inputField.value = longstring;  // Output: TypeError: Object [object Object] has no method 'setAttributes'  inputField.setAttributes("value", longstring); 
like image 213
F. Rakes Avatar asked Aug 30 '14 14:08

F. Rakes


People also ask

How do I enter values in Selenium without sendKeys?

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.

How do I replace text in Selenium?

Example: "is is is is is". replace('i','a'); -> This will replace the character 'i' in "is" string to 'a' ,so that the output string is "as as as as as".

Is sendKeys () a WebElement method?

What Is Selenium sendKeys() Method? Selenium provides sendKeys() method to input content in editable text fields or password fields in a webpage. These fields are like the typical web elements present on the web page that can be identified using any of the Selenium locators.


1 Answers

Try to set the element's value using the executeScript method of JavascriptExecutor:

WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')"); 
like image 161
Andrey Egorov Avatar answered Oct 06 '22 09:10

Andrey Egorov