Piyum Rangana
Piyum Rangana

Reputation: 71

how to paste a text value in input field with CTRL + v in Selenium Java?

I Want to past a text to a input field using CTRL + V in selenium Java. How to do it . Just I have a String So no need to copy a String from somewhere. I am trying to finding a way for it ?

Upvotes: 0

Views: 3318

Answers (2)

AmitKS
AmitKS

Reputation: 162

Actions Class : For handling keyboard and mouse events selenium provided Actions Class

keyDown(): This method simulates a keyboard action when a specific keyboard key needs to press.

keyUp(): The keyboard key which presses using the keyDown() method, doesn’t get released automatically, so keyUp() method is used to release the key explicitly.

sendKeys(): This method sends a series of keystrokes to a given web element.

    Actions action = new Actions(driver);
    action.keyDown(keys.CONTROL);    
    action.sendKeys("c");
    action.keyUp(keys.CONTROL);
    action.build().perform(); // copy is performed
    
    action = new Actions(driver);
    action.keyDown(keys.CONTROL);
    action.sendKeys("v");
    action.keyUp(keys.CONTROL);
    action.build().perform(); // paste is performed

Upvotes: 0

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

Assuming the string value is present in clipboard (using CTRL+C), you can retrive it as a string and pass on to your text field

  Toolkit toolkit = Toolkit.getDefaultToolkit();
            Clipboard clipboard = toolkit.getSystemClipboard();
            String copyFromClipboard= (String) clipboard.getData(DataFlavor.stringFlavor);
            System.out.println("String from Clipboard:" + result);
YourWebElement.sendkeys(copyFromClipboard);

Upvotes: 1

Related Questions