6

So I have multiple TextBoxes I need to fill and Sendkeys() takes a lot of time, is there a faster way to fill TextBoxes in Selenium?

  • Why it takes lots of time? – Peter M. Sep 25 '17 at 16:50
6

You can do all of that in a single script that you send via executeScript():

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(
    "document.getElementById('elementID1').setAttribute('value', 'value1');" + 
    "document.getElementById('elementID2').setAttribute('value', 'value2')");
);

This, on one hand would require a single selenium JSON over HTTP command, which should provide a serious performance boost. On the other hand, you are not on the selenium webdriver territory at this point - elements are located directly via getElementById, the sending keys is not simulated, but rather a value is directly set. There could be some related issue with that.

  • Note that this is not how a real user would enter data in text fields. If the page contains logic to validate or otherwise process data on input click/focus/change, it won't get triggered. – Blaise Apr 14 at 14:16
4

To add to the answer given by @alecxe, I wanted to point out that you can pass into the executed Javascript a WebElement that you have already located using the WebDriver. It would look something like the code below (note that I typically work in C# and the exact code provided has not been tested).

void SetElementText(WebDriver driver, WebElement textInput, String textValue) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].setAttribute('value', 'arguments[1]')", textInput, textValue);
}

To give a concrete example of the negative effects of doing this, consider the case where the text input that you have located is currently disabled. If you used sendKeys(), an exception would be thrown and the test would fail - this would be the desired result, because if you expect to be able to enter text into a field, and the field is disabled, the test should fail. However, if you set the value of the field using executed Javascript, the value will be successfully set, and the test will continue - your test may have just failed to identify a regression in your software.

If using this method, extra precautions should be taken to ensure that the element is in the expected state. I personally wouldn't recommend going this route simply for a performance improvement, unless you are dealing with an extreme case, like a page with dozens of fields.

3

You can use JavaScriptExecutor where you can use methods like getElementById and getElementByName:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

JavascriptExecutor myExecutor =   ((JavascriptExecutor) driver);
myExecutor.executeScript.   ("document.getElementsByName('q')[0].value='Kirtesh'");

driver.quit();
1

I had the same issue in python, tried all those 32-bit, 64-bit stuff with different browsers but send_keys() was taking incredibly long time and felt like an old grumpy man typing. I found the solution to use Javascript and boom that made a huge impact. Here is how you go around with using Javascript.

driver.execute_script('document.getElementById("content").value="My Dummy Text";')

Where:

Driver is your browser driver that you created using the below similar command:

driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_path)

execute_script is the function we'll use to pass the Javascript

document.getElementByID is your JS method using which you are going to grab the textbox/textarea information, in this case, since, we are using getElementByID, therefore you have to find the ID of the textbox, in my case it was "content", see reference below:

<textarea class="wp-editor-area" style="height: 361px; margin-top: 37px;" autocomplete="off" cols="40" name="content" id="content" aria-hidden="false"></textarea>

Notice the ID parameter of the above HTML textarea tag. You can find this information using Chrome Developer tools by performing right click and Inspect and then copying the element and then checking the ID information. If for some reason, ID is not present, you can use other methods such as 'Class' or 'Name' or 'CSS' etc. for getElementBy*

Refer to this link for more information : https://www.w3schools.com/js/js_htmldom_elements.asp

Finally, you are throwing in your content into that textbox using the .value parameter. In our case, we are passing the text "My Dummy Text" as a value to textbox and it works absolutely fine.

Hope this helps anyone looking to speed up send_keys() in python

Note: This method will replace all the existing text in the textarea, if you would like to preserve the existing text, then you can first 'get' the element value, append to your string and then pass the value method and that should work fine.

0

I think below might be better to use:

var JSExecute = (IJavaScriptExecutor)Chromedriver;                                
JSExecute.ExecuteScript("arguments[0].focus();", _UIControl[0]);                
JSExecute.ExecuteScript($"arguments[0].setAttribute('value', '{CrtlValue}')", _UIControl[0]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.