Python Selenium: Send keys is too slow Python Selenium: Send keys is too slow selenium selenium

Python Selenium: Send keys is too slow


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.


I had the same problem, did not fix it but found a way.

Copy the text to clipboard and paste it in the text input, that will be fast, first,

pip install xerox(or pyperclip)xerox.copy("anything")element.send_keys(Key.CONTROL,'v')

Hope this helps.


You can use pyperclip along with selenium. copy the string to pyperclip clipboard, paste it in the input field

    import pyperclip as pc    from selenium.webdriver.common.keys import Keys    pc.copy("Whatever text to paste")    element.send_keys(Keys.CONTROL, 'v')  #Paste using Ctrl+V