Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: How to prepend data to a text box or text Area

I am using Selenium Web driver. I have a Text Area where some text is written. Now How can I prepend some text/html in it or inserting a data at a specific location?

The following code appends it to the text Area/ Text Box

driver.findElement(By.xpath("textBox/textArea")).sendKeys("abc");

ie. if the text area/ text box contains 123. The result of above would be 123abc. But I want abc123 or 12abc3

PS: I am testing an "Email Reply" functionality. So as a user, when you reply to a mail, you do not do copy text, then clear all text then copy all the text back after writing the new text like below:

WebElement element = driver.findElement(By.xpath("textBox/textarea"));
String previousText = element.getAttribute("value");
element.clear();
element.sendKeys("abc" + previousText);

Please help...

like image 405
Bhuvan Avatar asked Dec 26 '22 14:12

Bhuvan


1 Answers

import org.openqa.selenium.Keys;

...

WebElement element = driver.findElement(By.xpath("textBox/textarea"));
element.sendKeys(Keys.HOME + "abc");

or may be for multiline text areas

element.sendKeys(Keys.CONTROL, Keys.HOME);
element.sendKeys("abc");
like image 171
Alexei Barantsev Avatar answered Jan 29 '23 04:01

Alexei Barantsev