Chris McDonald
Chris McDonald

Reputation: 73

Java WebDriver Copy text Issue

Good Afternoon,

So I am trying to Copy some text from a field so I can paste it somewhere else in my test.

public static void validateTestCaseCreated(){
    driver.findElement(By.xpath("//*[@id='mainForm:testTitle']")).click();
    Action builder;
    Actions copy = new Actions(driver);
    copy.sendKeys(Keys.CONTROL + "a");
    copy.sendKeys(Keys.CONTROL + "c");
    builder = copy.build();
    builder.perform();

The problem when it reaches line 6 it only sends c, it ignores the CONTROL. So my end result is not copying the text but highlighting the text then entering c.

Upvotes: 1

Views: 1360

Answers (2)

Brent Thoenen
Brent Thoenen

Reputation: 402

You could just copy the value from the text field into a variable and store it for use later.

Pull it from the page using your code along with the get attribute method.

String valueInField = driver.findElement(By.xpath("//*[@id='mainForm:testTitle']")).getAttribute("value");

That will grab the text from the field and put it into the variable for later use.

I'm not sure if this is doing fully what you are trying to do, seeing as you are trying to do a crtl+c, but this method is how to grab text using webdriver.

Upvotes: 1

Dingredient
Dingredient

Reputation: 2199

If your field is an input element, maybe you can do something like this instead:

driver.findElement(By.xpath("//*[@id='mainForm:testTitle']")).click().get_attribute("value");

Upvotes: 0

Related Questions