Reputation: 7611
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 Python bindings?)
Upvotes: 2
Views: 5456
Reputation: 14738
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.
Upvotes: 2
Reputation: 7611
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.
Upvotes: 4