driver.findElement(By.xpath("//*[@id='printshapesEdit-main']/div/div[2]/div/div[2]/input")).sendKeys(Keys.chord(Keys.CONTROL, "v"));

For the above code, I would like to retrieve the copied text to a string. Please do me favor.

  • 1
    You could try a simple web search. I found this tutorial (avajava.com/tutorials/lessons/…) for getting text from the clipboard on the first page of Google results.
    – Kate Paulk
    Nov 17, 2017 at 12:35
  • 5
    Is there a specific reason that you want to copy the text to the clipboard and then save it? If not, you should be able to call getAttribute("value") on the located WebElement to retrieve the current text value. Nov 17, 2017 at 15:29
  • 3
    Also worth pointing out is the fact that you have sent the keys CTRL+V, which typically is the keyboard shortcut for paste, not copy. Nov 17, 2017 at 23:29

6 Answers 6


I can't really see why you would use copy/paste to get the text from an element and then enter it into a field and since you haven't provided a reason for it here's an easier way:

Get text from an element:

public String getElementText(By locator) {
    WebElement locatorElement = driver.findElement(locator);
    return locatorElement.getText();

Enter text to a field:

public void enterTextIntoField(String text, By locator) {
    WebElement locatorElement = driver.findElement(locator);

please use below code

public void getClipboardContents() 
throws UnsupportedFlavorException, IOException {
String result = "google.com";
System.setProperty("webdriver.chrome.driver", "E:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String copy = Keys.chord(Keys.CONTROL,Keys.chord("c"));

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
String x = (String) contents.getTransferData(DataFlavor.stringFlavor);
int a= result.length();
int b = x.length();
System.out.println("Matched Character length")
 System.out.println("Issue In Character length");

  • 2
    Thanks for your contribution, Starlord! Could you elaborate on how this applies to what the OP is doing? Maybe I'm just not versed enough in Selenium specifics to see it...
    – corsiKa
    Feb 19, 2018 at 5:48
  • Another concern with this code: it assumes that the driver is running on the same machine as the automation script. Mar 11, 2020 at 10:25

In automation testing company we use below mentioned java code to read text from text file, and you can use this method directly where you want to enter text in filed.

    public static String getTextFromTextFile() {
     String sFilePath = "path to text file";
      //Creating FileReader object
      FileReader fr = null;
      //Creating BufferedReader object
      BufferedReader txtReader  = null;
      //Handling Exception using Try-Catch
      String sCurrentLine = null;
      try {

         fr =  new FileReader(sFilePath);
         txtReader = new BufferedReader(fr);
         //Reading file until file is null
         while ((sCurrentLine = txtReader.readLine()) != null) {

        } catch (IOException e) {
        } finally {
         try {
          if (txtReader != null)txtReader.close();
         } catch (IOException ex) {
        return sCurrentLine;

and if you want to enter text in text field you can use selenium function sendkeys(). Below is the code:

public string enterTextInField (){


Further you can use selenium functions getText() and sendkeys to get and enter text.

public string getTextOfElement(){
string elementText = driver.findElement(By.Locator).getText();
return elementText

public string enterTextInField (){

For testing functionality of a COPY BUTTON or to verify whether the right string is copied or not we can check it by pasting the string in any input field and then retrieve the text from that field and compare it accordingly to verify the results.

Note:-- Since we cannot directly get the text into our local variable by any action we need a web element to get our text.

For Pasting the text you can use:--

    Actions a = new Actions(driver); //to perform copy action
    driver.findElement(By.id("inputBoxId")).clear(); //if its already having any text
    driver.findElement(By.id("inputBoxId")).sendKeys(Keys.chord(Keys.CONTROL, "v")); //to perform paste operation in input box
    driver.findElement(By.xpath("//input[@value='Save']")).click(); //save the value else the value attribute of input box will not change.

    String getDataInLocalVar = driver.findElement(By.xpath("//*[@id='inputBoxId']")).getAttribute("value");
  • You can save your value in input box before clearing if you dont want to change it. You can save it later on.
  • Just pasting the value will not change anything so SAVE it then retrieve the text.

So basically, there is no straight way of doing this. Not sure why this was not thought out. A possible workaround is the following:

String copiedText = driver.findElement(By.xpath("//*[@id='printshapesEdit-main']/div/div[2]/div/div[2]/input")).getAttribute("value").toString();


Once you have issued a 'ctrl+v' to the input field, you can issue a .getAttribute() to the same input field and move it into a variable.

Hope this helps. Cheers!


This will work on c#.

        driver.FindElement(By.CssSelector("body")).SendKeys(OpenQA.Selenium.Keys.Control + "a");
        driver.FindElement(By.CssSelector("body")).SendKeys(OpenQA.Selenium.Keys.Control + "c");
        var txt = Clipboard.GetText();

I like this better then driver.FindElement(By.CssSelector("body")).Text, because Text returns all kinds of extra chars, this returns clean text. Works especialy well with tables returning tab delimted rows that can easily be pasted into excel.

  • Please note that this assumes that the driver is running on the same machine as the automation script. Mar 11, 2020 at 10:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.