|
| 1 | +# How to work with clipboard in Selene? {: #clipboard-copy-and-paste-howto} |
| 2 | + |
| 3 | +{% include-markdown 'warn-from-next-release.md' %} |
| 4 | + |
| 5 | +## Summary |
| 6 | + |
| 7 | +Selene works with clipboard via the [pyperclip](https://pypi.org/project/pyperclip/) package. And there is only one command in Selene that uses it under the hood – [command.paste(text)][selene.core.command.paste]. In other cases you can use `pyperclip` directly to achieve your goals via `paperclip.copy(text)` and `paperclip.paste(text)`. |
| 8 | + |
| 9 | +Let's consider the main scenarios to work with clipboard... |
| 10 | + |
| 11 | +## Main Scenarios |
| 12 | + |
| 13 | +### Copy a text into clipboard |
| 14 | + |
| 15 | +```python |
| 16 | +import pyperclip |
| 17 | +... |
| 18 | + |
| 19 | +pyperclip.copy('text to copy') |
| 20 | +``` |
| 21 | + |
| 22 | +### Copy a text of element into clipboard |
| 23 | + |
| 24 | +```python |
| 25 | +from selene import browser, query |
| 26 | +import pyperclip |
| 27 | +... |
| 28 | + |
| 29 | +pyperclip.copy(browser.element('.message').get(query.text)) |
| 30 | +``` |
| 31 | + |
| 32 | +See also [#573](https://github.com/yashaka/selene/issues/573) |
| 33 | + |
| 34 | +### Copy a value of an input element into clipboard |
| 35 | + |
| 36 | +```python |
| 37 | +from selene import browser, query |
| 38 | +import pyperclip |
| 39 | +... |
| 40 | + |
| 41 | +pyperclip.copy(browser.element('input').get(query.value)) |
| 42 | +``` |
| 43 | + |
| 44 | +### Copy currently selected text on the page into clipboard via OS-based shortcut |
| 45 | + |
| 46 | +```python |
| 47 | +from selene import browser, command |
| 48 | +... |
| 49 | + |
| 50 | +browser.perform(command.copy) |
| 51 | +``` |
| 52 | + |
| 53 | +See also [#575](https://github.com/yashaka/selene/issues/575) |
| 54 | + |
| 55 | +### Paste into currently focused element a text from the clipboard via OS-based shortcut |
| 56 | + |
| 57 | +```python |
| 58 | +from selene import browser, command |
| 59 | +... |
| 60 | + |
| 61 | +browser.perform(command.paste) |
| 62 | +``` |
| 63 | + |
| 64 | +See also [#575](https://github.com/yashaka/selene/issues/575) |
| 65 | + |
| 66 | +### Put a text in the clipboard, then paste it into currently focused element via OS-based shortcut |
| 67 | + |
| 68 | +```python |
| 69 | +from selene import browser, command |
| 70 | +... |
| 71 | + |
| 72 | +browser.perform(command.paste('some text to put into clipboard first')) |
| 73 | +``` |
| 74 | + |
| 75 | +See also [#575](https://github.com/yashaka/selene/issues/575) |
| 76 | + |
| 77 | +### Select value of an input element, then copy it into clipboard via OS-based shortcut |
| 78 | + |
| 79 | +```python |
| 80 | +from selene import browser, command |
| 81 | +... |
| 82 | + |
| 83 | +browser.element('input').select_all().copy() |
| 84 | +# OR: |
| 85 | +browser.element('input').select_all().perform(command.copy) |
| 86 | +``` |
| 87 | + |
| 88 | +### Type a text stored in the clipboard into text input at the current cursor position |
| 89 | + |
| 90 | +```python |
| 91 | +from selene import browser |
| 92 | +import pyperclip |
| 93 | +... |
| 94 | + |
| 95 | +browser.element('input').type(pyperclip.paste()) |
| 96 | +``` |
| 97 | + |
| 98 | +### Set a new value to the text input getting this value from the clipboard |
| 99 | + |
| 100 | +```python |
| 101 | +from selene import browser |
| 102 | +import pyperclip |
| 103 | +... |
| 104 | + |
| 105 | +browser.element('input').set_value(pyperclip.paste()) |
| 106 | +``` |
| 107 | + |
| 108 | +### Paste via OS-based shortcut a text stored in the clipboard into text input at the current cursor position |
| 109 | + |
| 110 | +```python |
| 111 | +from selene import browser, command |
| 112 | +... |
| 113 | + |
| 114 | +browser.element('input').paste() |
| 115 | +# OR: |
| 116 | +browser.element('input').perform(command.paste) |
| 117 | +``` |
| 118 | + |
| 119 | +### Paste via OS-based shortcut a text stored in the clipboard into text input substituting the current text via "select all" OS-based shortcut |
| 120 | + |
| 121 | +```python |
| 122 | +from selene import browser, command |
| 123 | +... |
| 124 | + |
| 125 | +browser.element('input').select_all().paste() |
| 126 | +# OR: |
| 127 | +browser.element('input').select_all().perform(command.paste) |
| 128 | +``` |
| 129 | + |
| 130 | +### Copy a text into clipboard, then paste it via OS-based shortcut into text input at the current cursor position |
| 131 | + |
| 132 | +```python |
| 133 | +from selene import browser, command |
| 134 | +... |
| 135 | +text = 'text to append' |
| 136 | +browser.element('input').paste(text) |
| 137 | +# OR: |
| 138 | +browser.element('input').perform(command.paste(text)) |
| 139 | +``` |
| 140 | + |
| 141 | +### Copy a text into clipboard, then paste it via OS-based shortcut into text input substituting the current text via "select all" OS-based shortcut |
| 142 | + |
| 143 | +```python |
| 144 | +from selene import browser, command |
| 145 | +... |
| 146 | +text = 'new text value' |
| 147 | +browser.element('input').select_all().paste(text) |
| 148 | +# OR: |
| 149 | +browser.element('input').select_all().perform(command.paste(text)) |
| 150 | +``` |
0 commit comments