Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium: Send keys is too slow

I'm using element.send_keys("Anything") to fill out a form, but it takes very much time and the script has to fill it in fast.

I tried different Chromedriver versions but nothing helped. Do you have any ideas why it takes so long? How can I make it faster?

Note: I'm searching for xpath. If I try searching for ID or name I get an error. I don't know if that would be faster.

like image 230
Julian Avatar asked Apr 21 '18 13:04

Julian


People also ask

What is send keys in selenium Python?

What Is SendKeys? SendKeys is a method used to send keyboard input such as characters, numbers, and symbols to text boxes inside an application. When you are testing an application, all the actions are taken care of by the WebDriver element, which sendKeys is a part of.

Why is selenium execution slow?

Generally, Selenium WebDriver scripts are very slow because they run through the browser.


2 Answers

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
like image 87
Nelwin Avatar answered Nov 10 '22 11:11

Nelwin


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.

like image 31
PanDe Avatar answered Nov 10 '22 11:11

PanDe