Ambamamba
Ambamamba

Reputation: 41

Copy text in python and paste by Ctrl + V in webdriver

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

Upvotes: 1

Views: 3036

Answers (2)

JeffC
JeffC

Reputation: 25731

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.

Upvotes: 0

Prophet
Prophet

Reputation: 33361

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

Upvotes: 3

Related Questions