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?
You can do all of that in a single script that you send via
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.
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
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.
You can use
driver.execute_script('document.getElementById("content").value="My Dummy Text";')
Driver is your browser driver that you created using the below similar command:
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_path)
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.