Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I edit a text field in Selenium 2?

I can type text into a field using WebElement.sendKeys() but editing doesn't work: I can neither move the cursor nor delete the last character that I typed with e.sendKeys( Keys.BACK_SPACE )

How do I modify the value of a text field in Selenium 2 (WebDriver)?

like image 731
Aaron Digulla Avatar asked Oct 26 '11 12:10

Aaron Digulla


People also ask

How do I edit text in Selenium?

Selenium can be used to input text to an edit box. An edit box is represented by the input tag and its type attribute should have the value as text. It can be identified with any of the locators like - id, class, name, css, xpath and tagname. To input a value into an edit box, we have to use the method send_keys.

How do I insert text into a text box in Selenium?

We can type in a textbox using Selenium webdriver. We shall use the sendKeys() method to type in the edit box. It is an in-built method in Selenium.

How do I add text to a textbox without sendKeys?

We can input text in the text box without the method sendKeys with thehelp of the JavaScript Executor. Selenium executes JavaScript commands with the help of the executeScript method. The JavaScript command to be run is passed as parameter to the method.

How do I remove a value from a field in Selenium?

We can enter text on any field in Selenium. After entering the text, we may need to remove or clear the text we entered in that field. This interaction with the web elements can be achieved with the help of clear() method. Thus a clear() method is used to clear or reset an input field belonging to a form/ edit box.

What is xOffset and yOffset in Selenium?

Both xOffset and yOffset represent the relative pixel distance (integer) with which to scroll. For xOffset , positive value means to scroll right, and negative value means to scroll left. For yOffset , positive value means to scroll downward, and negative value means to scroll upward.

What is the command to getText for a field?

Using getText() Method To Get A Dropdown Text We can use the getText() method to handle such scenarios as well.


2 Answers

You can definitely do that by either of the two methods. I have tried and it works.

e.click()   # Positions the cursor at the end of the string
e.sendKeys(Keys.BACK_SPACE )

Or you could simply clear the text, and start over again:

e.clear()
e.sendKeys("What you want to send")
like image 76
rsmoorthy Avatar answered Sep 24 '22 03:09

rsmoorthy


I found this solution that seems to work pretty well. It basically clicks on the text field WebElement, then sends Ctrl-End to put the cursor at the end of the text. Then sends the string that I had previously initialized.

(quickReplyTextArea is a text field WebElement that I have previous found, as is postQuickReplyButton (button instead of text field, obviously). replyText is a String that I initialized earlier)

    quickReplyTextArea.click();
    quickReplyTextArea.sendKeys(Keys.chord(Keys.CONTROL, Keys.END));
    quickReplyTextArea.sendKeys(replyText);
    postQuickReplyButton.click();
like image 37
alphonzo79 Avatar answered Sep 25 '22 03:09

alphonzo79