How do I clear a textbox before writing to it? Using Webdirver C# and there is no way to use Keys.Chord

19,972 views
Skip to first unread message

tommyboy

unread,
Apr 14, 2011, 7:23:56 PM4/14/11
to webdriver
Hello,

I was wondering if there was a way to clear a textbox/textfield using
C# Webdriver (selenium-dotnet-2.0b2).
If the textbox initially is empty there's no problem but if there is
text in it already Sendkeys just appends to what's existing.

I know that you can Keys.Chord together "Control" and "a" to select
all, then send Keys.Delete to delete the existing text but Keys.Chord
does not seem to have been written for C# (WebDriver.Common.dll
v2.0.50727).

Is there any other way of doing this?

THANK YOU!!

tommyboy

unread,
Apr 14, 2011, 7:36:16 PM4/14/11
to webdriver
Or even better, is there a way to avoid Sendkeys altogether and just
have it instantaneously replace the current text in a textbox with the
string of your choice?
With Sendkeys you need to wait for each character to be deleted/typed
out and if you're filling in a textbox/textfield with a lot of
characters, this slows down automation immensely.

I've seen this before in other types of automation such as Watin but
do not know if Webdriver has the same functionality.

Again, thanks in advance!!
-tommy

Luke Inman-Semerau

unread,
Apr 14, 2011, 7:52:01 PM4/14/11
to webd...@googlegroups.com, webdriver
The sendKeys is 'slow' because it issues events for every letter
(keydown, keyup, keypress). If you don't care about those events and
want to speed up your test then you could always use javascript to
update the text.

Also you have to clear() the text field first if you don't want it to
append.

-Luke

> --
> You received this message because you are subscribed to the Google
> Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en
> .
>

Bill Ross

unread,
Apr 14, 2011, 8:11:41 PM4/14/11
to webd...@googlegroups.com
> The sendKeys is 'slow' because it issues events for every letter
> (keydown, keyup, keypress). If you don't care about those events and
> want to speed up your test then you could always use javascript to
> update the text.

If Webdriver's goal is to strictly emulate user actions, shouldn't there
be the equivalent of pasting text? I don't see it in WebElement or
RenderedWebElement.

Bill

tommyboy

unread,
Apr 14, 2011, 8:15:19 PM4/14/11
to webdriver
I see, that makes sense.
Apologies in advance because I'm never used javascript before!

If my textbox had an ID of "mytextbox" and I wanted to write,
"Burrito", would you be able to show me how to use javascript in a
Webdriver script to clear the text field and update the text??

Thank you!
-tommy

Matt

unread,
Apr 14, 2011, 8:16:52 PM4/14/11
to webd...@googlegroups.com
Clear the element before typing:
var element = driver.FindElement(....);
element.Clear();

tommyboy

unread,
Apr 14, 2011, 8:23:10 PM4/14/11
to webdriver
Matt you rock.

I don't know how I could have missed that. Thank you very much kind
sir =)

Daniel Wagner-Hall

unread,
Apr 16, 2011, 11:32:44 PM4/16/11
to webd...@googlegroups.com
Meta-characters are automatically chorded to the next character you
press, so if you do (uncompiled/untested code):

element.SendKeys(Keys.Control + "v");

it will paste the contents of your clipboard, if you:

element.SendKeys(Keys.Control + "a");

it will select all.

Also, if you want to manually set the contents of the text field by
javascript (ignoring events), you can:

((IJavascriptExecutor)driver).ExecuteScript("arguments[0].value =
'';", element);

Rami Rihawi

unread,
Feb 15, 2017, 6:00:02 AM2/15/17
to webdriver
Thanks
Reply all
Reply to author
Forward
0 new messages