1

I have written the following.

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys
bot = webdriver.Firefox()
bot.find_element_by_name("username").send_keys(config['username'])

When I am using send_keys and happen to be typing at the same instant, then what I typed is also added in the username.
How to avoid this?

Example:

I want to fill the username with "sandeep" If at the same instant I press 'a', then the username becomes "sandeepa" or something equivalent.

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
Sandeep
  • 155
  • 1
  • 14
  • 3
    I doubt that there would be a simple solution for that. Do you have to type when this script runs? – DeepSpace Aug 18 '16 at 14:01
  • This seems more like a limitation of the OS/Browser and Selenium than an actual issue. As DeepSpace added, do you _need_ to type at the same time as the test is running? – Dillanm Aug 18 '16 at 14:21
  • I used it to download and sync some files so it would be running at the back and I am using a cron job to run it so the simple solution is not used. – Sandeep Aug 18 '16 at 21:00

2 Answers2

1

You can use executeScript method:

webdriver.execute_script("document.getElementById('username').setAttribute('value', 'Sandeep')")

JavaScript will do text insertion as single operation.

Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
0

I see 2 options:

  1. Create hidden input send keys to it than perform copy/paste from hidden to visible input, after remove hidden input.

  2. Hide input, than send_keys to it and after show it back.

Usefull links:

Performing a copy and paste with Selenium 2

WebDriver: add new element

Community
  • 1
  • 1
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82