9

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.

Julian
  • 101
  • 1
  • 5
  • 2
    How slow is it going, and how fast do you need it to be? Also, if you can post a [mcve], that would be helpful. – Michal Charemza Apr 21 '18 at 14:17
  • Questions seeking debugging help ("**Why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](https://stackoverflow.com/help/mcve) – undetected Selenium Apr 21 '18 at 18:21
  • Double check the web driver version and the version chrome. Check the bit rates 32 or 64 bit driver and browser. I have had this problem with IE, but not Chrome and there was a versioning issue. Changing the versions seemed to help. – Nicholas Martinez Apr 22 '18 at 14:19
  • I noticed that its just slow on the site i want to fill out the form. On any other site send_keys is as fast as i want it to be. – Julian Apr 23 '18 at 19:37

4 Answers4

11

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
Nelwin
  • 121
  • 1
  • 3
4

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.

PanDe
  • 831
  • 10
  • 21
3

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.

aritra
  • 31
  • 2
1

You can use Javascript to type the textbox

((JavascriptExecutor) driver).executeScript("arguments[0].value = arguments[1];", driver_tmp.findElement(By.id("text_id")), text);
Hypnoz
  • 1,115
  • 4
  • 15
  • 27