9

I am trying to select all the text in a textbox in order to clear the textbox. I am using Ctrl+A to do this using the following Python 2.7 code on Selenium RC standalone 2.20.0.jar Server on Windows 7 firefox:

from selenium import selenium
s = selenium('remote-machine-ip', 4444, '*chrome', 'http://my-website-with-textbox')
locator = 'mylocator-of-textbox'
s.open()
s.type(locator, 'mytext')
s.focus(locator)
s.control_key_down()
s.key_down(locator, "A")
s.key_press(locator, "A")
s.key_up(locator, "A")
s.control_key_up()

# Nothing happens here... I cannot see the text getting selected...

# Nothing gets cleared here except the last char
s.key_down(locator, chr(8))  # Pressing backspace
s.key_press(locator, chr(8))
s.key_up(locator, chr(8))

Any help? Thanks, Amit

4 Answers 4

11

I'm using clear() in WebDriver without any hassle...

el = self.selenium.find_element_by_name(name)
el.clear()
Sign up to request clarification or add additional context in comments.

4 Comments

I am not using WebDriver, because I am using Selenium RC... Any idea how to do the same in Selenium RC... I don't want to move to WebDriver now...
If .clear is not present in RC (I think it is), how about .type("") ?
I tried .type("A") in place of .key_down/press/up().... But I get the following exception: type not supported immediately after call to controlKeyDown() or altKeyDown() or metaKeyDown()
In my case clearing the textbox didn't help because the website automatically re-enters 0,00 if the textfield is cleared. While this might have been what op needed, this is not what he asked in the question title.
1

You can do this:

 public void selectAll(WebElement element) {
    String selectAll = Keys.chord(Keys.CONTROL, "a");
    element.sendKeys(selectAll);
}

And when you whant to use it, for example:

    selectAll(myDriver.findElement(By.id("testId")));

In this example the WebElement can be a text box, text area and similars.

2 Comments

This is not Python though. Could you maybe link the documentation so that people can at least understand the code if they can't copy-paste it
0

In Selenium RC, simply use the following to clear a textbox

selenium.type("someLocator", "");

Comments

0

Try using first

element.click()

then use element

element.clear()

It might solve your issue as it really solved mine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.