14

I am trying to type a float number into a textbox with default value 0.00.But it tries to get appended instead of overwriting it.I tried with .clear() and then send_keys('123.00') but still it gets appended. Then i tried with send_keys(Keys.CONTROL+'a','123.00').It updates 0.00 only.

Any help is really appreciated.

For more info .. URL : http://new.ossmoketest.appspot.com userid: senthil.arumugam@mycompanyname.com -- mycompanyname = orangescape (sorry to avoid spam mails) password not needed now. click purchaseorder... in the form please new product and new price... sample application for automation.. thanks

senthil3569
  • 471
  • 2
  • 5
  • 7

4 Answers4

20

I've had good results with:

from selenium.webdriver.common.keys import Keys

element.send_keys(Keys.CONTROL, 'a')
element.send_keys('123.00')

If that doesn't work it may have something to do with the code in the web page.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Glenn
  • 7,262
  • 1
  • 17
  • 23
7

Unless you have custom editbox, click() should work for you:

from selenium.webdriver import Firefox

b = Firefox()
b.get('http://google.com')
e = b.find_element_by_id('lst-ib')

e.click()  # is optional, but makes sure the focus is on editbox.
e.send_keys('12.34')
e.get_attribute('value')
# outputs: u'12.34'

e.click()
e.clear()
e.get_attribute('value')
# outputs: u''

e.send_keys('56.78')
e.get_attribute('value')
# outputs: u'56.78'
Misha Akovantsev
  • 1,817
  • 13
  • 14
3

I just found the clear() command - see here:

If this element is a text entry element, this will clear the value. Has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements.

EDIT: So your approach would be:

   element.clear();
   element.sendKeys('123.00');
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
2

I've experienced issues with all the examples given in other answers.

el.send_keys(Keys.CONTROL + 'a' + Keys.NULL, 'your string')

Has worked in all the projects I've worked in, so much I've wrapped it into my own implementation of the Webdriver class with more robust operations.

thodic
  • 2,229
  • 1
  • 19
  • 35