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.
-
3I doubt that there would be a simple solution for that. Do you have to type when this script runs?DeepSpace– DeepSpace2016年08月18日 14:01:45 +00:00Commented Aug 18, 2016 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– Dillanm2016年08月18日 14:21:46 +00:00Commented Aug 18, 2016 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– Sandeep2016年08月18日 21:00:34 +00:00Commented Aug 18, 2016 at 21:00
2 Answers 2
You can use executeScript method:
webdriver.execute_script("document.getElementById('username').setAttribute('value', 'Sandeep')")
JavaScript will do text insertion as single operation.
Comments
I see 2 options:
Create hidden input send keys to it than perform copy/paste from hidden to visible input, after remove hidden input.
Hide input, than send_keys to it and after show it back.
Usefull links: