Skip to content Skip to sidebar Skip to footer

Direct Clipboard Manipulation With Selenium Webdriver

Is it possible to use Selenium WebDriver to pre-populate the clipboard with some text to be pasted, as though the text had been copied in another application? (Ideally using the Py

Solution 1:

No, it seems not – as a browser manipulation tool, Selenium is designed to perform functions unique to browsers. In general, clipboard manipulation is a function handled by a file manager, usually one which includes a GUI, such as Windows Explorer or the Mac OS Finder.

There are ways for the various Selenium interfaces (Java, Python, etc) to access clipboard functions, but these only work if the browser in question is running in a context which contains them. If Selenium is running headless (meaning with no GUI, e.g. using a virtual display such as Xvfb, possibly as part of a virtual machine) there may not be any accessible context with clipboard functionality, and Selenium itself does not provide any on its own.

Solution 2:

A hack you can do to "pre-populate" the clipboard is to perform the steps a user would take to copy it into the clipboard.

one way to do this (assuming you have some text you want to copy), is to open a url containing the text, press the keys 'ctrl-a' while the body has the focus, then press the keys 'ctrl-c'

driver.findElements(By.tagname("body")).type(Keys.chord(keys.control, 'a'));
driver.findElements(By.tagname("body")).type(Keys.chord(keys.control, 'c'));

you might have to put a sleep between each step, so that the OS has time to actually perform the copy operation - i find that sometimes, selenium runs too fast.

Post a Comment for "Direct Clipboard Manipulation With Selenium Webdriver"