1

I am trying to input an amount in a text field which contains a default string.

HTML of that field:

<div class="InputGroup">
    <span class="InputGroup-context">$</span>
    <input autocomplete="off" class="Input InputGroup-input" id="amount" name="amount" type="text" maxlength="12" value="0.00">
</div>

When trying to input text to the field, instead of replacing the default text, it appends it to it.

I have tried to use amount.clear() (amount is what I am calling the element) but after running that and sending the keys it throws the below exception:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

As you will see from my current code, I am also trying to double click the default text but that doesn't help.

This is my code at the moment:

ActionChains(driver).move_to_element(amount).click(amount).click(amount).perform()
amount.send_keys(Keys.BACKSPACE)
amount.send_keys('100')

Which results in an input field of 0.00100 when I'm expecting 100.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Foxtrot
  • 53
  • 1
  • 1
  • 5
  • If what has already been suggested does not work, you can always send `keys.BACKSPACE` to clear the textbox. – Glazbee Feb 14 '19 at 15:10
  • 1
    Do you understand what a `StaleElementReferenceException` is and what causes it? – JeffC Feb 14 '19 at 16:18
  • check this link. It might help https://stackoverflow.com/questions/12967541/how-to-avoid-staleelementreferenceexception-in-selenium – Ankit Agrawal Feb 14 '19 at 16:49

1 Answers1

0

Some JS based webs apps may not clear the input fields properly.

You can try using Actions class to click and clear a field fully. For example:

elem = driver.findElement(By.id("amount"))
actions.move_to_element(elem).click().build().perform()
actions.send_keys(Keys.BACKSPACE)
       .send_keys(Keys.BACKSPACE)
       .send_keys(Keys.BACKSPACE)
       .send_keys(Keys.BACKSPACE).build().perform()
Naveen
  • 770
  • 10
  • 22