1
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.

3
  • 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
    Commented 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. Commented 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. Commented Nov 17, 2017 at 23:29

6 Answers 6

7

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);
    locatorElement.click();
    locatorElement.clear();
    locatorElement.sendKeys(text);
}
1

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();
driver.get("https://www.google.co.in/");
String copy = Keys.chord(Keys.CONTROL,Keys.chord("c"));
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("google.com");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys(Keys.CONTROL+"a");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys(copy);

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

    }
2
  • 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
    Commented 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. Commented Mar 11, 2020 at 10:25
0

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() {
      //FilePath
     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) {
           System.out.println(sCurrentLine);
         }

        } catch (IOException e) {
             e.printStackTrace();
        } finally {
         try {
          if (txtReader != null)txtReader.close();
         } catch (IOException ex) {
          ex.printStackTrace();
         }
        }
        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 (){
driver.findElement(By.Locator).clear();
driver.findElement(By.Locator).sendKeys(getTextFromTextFile());

}

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 (){
driver.findElement(By.Locator).clear();
driver.findElement(By.Locator).sendKeys(getTextOfElement());
} 
0

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.
0

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();

System.out.println(copiedText);

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!

0

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.

1
  • Please note that this assumes that the driver is running on the same machine as the automation script. Commented 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.