Can't clear input field using IWebDriver.Clear() method

12,959 views
Skip to first unread message

Volodymyr Stelmakh

unread,
May 25, 2012, 9:18:00 AM5/25/12
to seleniu...@googlegroups.com
Hello 

I use selenium v2.21 & FF11. 
Task: to clear field before sending a value.

How do field looks on the page:
<input type="text" maxlength="128" name="amount" id="edit-amount" size="4" value="265" tabindex="1" class="form-text form-element-type-textfield ClickTaleSensitive" /> 

How do i do it:
    element.Clear();
    element.SendKeys(value);

When i run this code, it writes value to the right of the existing.

What do i do wrong?

Thanks

Stefan Teixeira

unread,
May 26, 2012, 11:27:43 AM5/26/12
to seleniu...@googlegroups.com
I don't know why the clear() method isn't working for you, but I faced a situation like that recently and I used the Keys.chord method to solve that (see selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/Keys.html).

You could try to select the content of the field by pressing CTRL + A, it should look like element.sendKeys(Keys.chord(Keys.CONTROL, "a"), value)).

If this doesn't work, try something like element.sendKeys(Keys.chord(Keys.END, Keys.BACKSPACE, Keys.BACKSPACE, ... (enough backspaces to erase the field)), value)).


2012/5/25 Volodymyr Stelmakh <vk.st...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/Z0vRMhIHyTkJ.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

lbrown

unread,
Dec 15, 2014, 3:03:59 PM12/15/14
to seleniu...@googlegroups.com
This is a really old post so I am hoping someone will still have an answer for me.

I am having a problem clearing default data from a field with the clear() method in Selenium. SO I followed the example below using the Keys methothds and got the following error:

NameError: uninitialized constant "filename"::Keys

I am not a developer and have little experience with initializing classes. How do I initialize "Keys", and is that all I need to do to enable the following to execute correctly?

@driver.find_element(:id, "txtBrokerCommission").send_keys(Keys.BACK_SPACE)
    @driver.find_element(:id, "txtBrokerCommission").send_keys(Keys.chord(Keys.CONTROL, "a"))
    @driver.find_element(:id, "txtBrokerCommission").send_keys(Keys.DELETE)
    @driver.find_element(:id, "txtBrokerCommission").send_keys "100.00"

Emily Zhai

unread,
Aug 30, 2016, 12:19:58 AM8/30/16
to Selenium Users
I'm probably two years too late but I'm guessing you forgot to import. Add from selenium.webdriver.common.keys import Keys to the top of your document.

Selenium Curious

unread,
Mar 3, 2018, 4:26:12 AM3/3/18
to Selenium Users
Hi how to clear all default values from a form using slelenium webdriver. Please help me out immediately if possible as i am new to this coding

Xiang Dong

unread,
Mar 5, 2018, 10:06:14 AM3/5/18
to Selenium Users
Maybe you can click it before send keys. It depends on how does your web developed.

________________________________________
From: seleniu...@googlegroups.com <seleniu...@googlegroups.com> on behalf of Selenium Curious <kanchan...@gmail.com>
Sent: Saturday, March 3, 2018 2:38:13 AM
To: Selenium Users
Subject: [selenium-users] Re: Can't clear input field using IWebDriver.Clear() method
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com<mailto:selenium-user...@googlegroups.com>.
To post to this group, send email to seleniu...@googlegroups.com<mailto:seleniu...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/f35ec953-1289-49df-876f-ada77696a534%40googlegroups.com<https://groups.google.com/d/msgid/selenium-users/f35ec953-1289-49df-876f-ada77696a534%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

poobathy r

unread,
Mar 5, 2018, 10:13:51 AM3/5/18
to seleniu...@googlegroups.com
You can use java script clear that will clear the input box but this is  work around... 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/f35ec953-1289-49df-876f-ada77696a534%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages