1

I have one site where I can't enter a password using send_keys , I have to use Ctrl+V there. But I don't know how this can be done, because I have Passwords, where all passwords are stored and I need to take it from there

from Passwords import password

#how to copy?

inputpassword = driver.find_element(By.XPATH, "Path")
inputpassword.???

#how to paste by Ctrl + V
Prophet
  • 32,350
  • 22
  • 54
  • 79
Ambamamba
  • 41
  • 8
  • what site makes this problem? maybe it needs to put value in different widget, or maybe it needs to send it using JavaScript – furas Aug 28 '22 at 22:07
  • I needed to automatically enter a mnemonic, which consists of 12 words, if you enter using "Ctrl" + "v", then the words are distributed independently over 12 cells. If send_keys is used, then all 12 words are entered into one cell. Or you would have to write code for each cell separately, which would take a lot of time – Ambamamba Aug 28 '22 at 22:14
  • I'm not interesting what you want to do but I would like to see this page. And usually code can be repeated using `for`-loop and it doesn't need so much time. – furas Aug 28 '22 at 22:18
  • Unfortunately it is not possible to provide a link. This is a Metamask extension. – Ambamamba Aug 28 '22 at 22:26
  • pity, it is interesting case – furas Aug 28 '22 at 22:30

2 Answers2

3

There are 2 steps here:

  1. Read the text from some file into the clipboard.
  2. Paste from the clipboard into the web element.
    There are several ways you can read a text from file into the clipboard, for example
import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')

More examples and explanations can be find here and here.
Then you can simply paste the clipboard content into a web element input with Selenium with regular CTRL + V keys

//now paste your content from clipboard
el = self.driver.find_element_by_xpath(xpath)
el.send_keys(Keys.CONTROL + 'v')

More examples and explanations can be found here or here

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

I know you already accepted an answer but there's no reason to put anything into the clipboard if you are reading the password from a file.

password = ... #read password string from file
inputpassword = driver.find_element(...)
inputpassword.send_keys(password)

or just simply

password = ... #read password string from file
driver.find_element(...).send_keys(password)

Pasting in the text offers no advantages only more code which means more places where things might go wrong.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thanks for the answer, but "Ctrl" + "v" was important for me, because send_keys could't be used in my case – Ambamamba Aug 28 '22 at 21:15
  • @Ambamamba Why can't `send_keys()` be used? You never mentioned this in your question. – JeffC Aug 29 '22 at 01:01